Main()和命令行参数

Main方法是程序的入口点,您将在那里创建对象和调用其他方法。(注:一个C#程序中只能有一个入口点)

class  TestClass {
        
static void Main(string[] args){
                
//Display the number of command line arguments:
                System.Console.WriteLine(args.Length);
            }

    }

概述:

l         Main方法是程序的入口点,程序控制在该方法中开始和结束。

l         该方法在类或结构的内部声明。它必须为静态方法,而不应为公共方法(Main方法接受默认访问级别private)

l         可以具有voidint返回类型。

l         声明Main方法时即可以使用参数,也可以不使用参数。

l         参数可以作为从零开始索引的命令行参数来读取。

l         CC++不同,程序的名称不会被当作第一个命令行参数。

 

主要内容:

l         命令行参数

l         如何:显示命令行参数

l         如何:使用foreach访问命令行参数

l         Main()返回值

 

 

命令行参数

Main方法可以使用参数,如:

static int Main (string[] args)

static void Main (string[] args)

Main方法的参数是表示命令行参数的string数组。通常通过测试Length属性来检查参数是否存在。

示例:

在此示例中,程序在运行时采用一个参数,将该参数转换为整数,并计算该数的阶乗。如果没有提供参数,则程序发出一条消息来解释程序的正确用法。

class  Functions {
        
public static long Factorial(int n){
                
if (n < 0){        //error result - undefined
                        return -1;
                    }

                
                
if (n > 256){        //error result - input is too big
                        return -2;
                    }

                
                
if (n == 0){
                        
return 1;
                    }

                
                
//calculate the factorial iteratively rather than recursively:
                long tempResult = 1;
                
for (int i = 1; i <= n; i++){
                        tempResult 
*= i;
                    }

                
                
return tempResult;
                
            }

    }


class  MainClass
{
    
static int Main(string[] args)
    
{
        
// Test if input arguments were supplied:
        if (args.Length == 0)
        
{
            System.Console.WriteLine(
"Please enter a numeric argument.");
            System.Console.WriteLine(
"Usage: Factorial <num>");
            
return 1;
        }


        
try
        
{
            
// Convert the input arguments to numbers:
            int num = int.Parse(args[0]);

            System.Console.WriteLine(
"The Factorial of {0} is {1}.", num, Functions.Factorial(num));
            
return 0;
        }

        
catch (System.FormatException)
        
{
            System.Console.WriteLine(
"Please enter a numeric argument.");
            System.Console.WriteLine(
"Usage: Factorial <num>");
            
return 1;
        }

    }

}


如何:显示命令行参数

可以通过Main的可选参数来访问通过命令行提供给可执行文件的参数。参数以字符串数组的形式提供。数组的每个元素都包含一个参数。参数之间的空白被移除。

示例:

本示例显示了传递给命令行应用程序的命令行参数。

class  CommandLine {
        
static void Main(string[] args){
                
//The Length property provides the number of array elements
                System.Console.WriteLine("parameter count = {0}",args.Length);
                
for (int i = 0; i < args.Length; i++){
                        System.Console.WriteLine(
"Arg[{0}] = [{1}]",i,args[i]);
                    }

            }

    }



 

命令行输入

传递给Main的字符串数组

CommandLine a b c

“a”

“b”

“c”

CommandLine one two

“one”

“two”

CommandLine “one two” three

“one two”

“three”

 

如何:使用foreach访问命令行参数

循环访问数组的另一种方法是使用foreach语句。foreach语句可以用于循环访问数组、.NET Framework集合类或任何实现IEnumerable接口的类或结构。

示例

下面的示例演示如何使用foreach输出命令行参数

class  CommandLine2 {
        
static void Main(string[] args){
                System.Console.WriteLine(
"Number of command line parameters = {0}",args.Length);
                
                
foreach (string s in args){
                        System.Console.WriteLine(s);
                    }

            }

    }


Main()返回值

Main方法返回值可以使void类型:

static   void  Main()
{
    
//
}


也可以是int类型:

static   int  Main()
{
    
//
}


如果不需要使用Main的返回值,则返回void可以使代码变得略微简单。但是,返回整数可使程序将状态信息与调用该可执行文件的其他程序或脚本相关。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值