logo search
CSharp_Prog_Guide

Общие сведения

Command-Line Arguments

The Main method can use arguments, in which case, it takes one of the following forms:

static int Main(string[] args)

static void Main(string[] args)

The parameter of the Main method is a String array that represents the command-line arguments. Usually you check for the existence of the arguments by testing the Length property, for example:

if (args.Length == 0)

{

System.Console.WriteLine("Please enter a numeric argument.");

return 1;

}

You can also convert the string arguments to numeric types by using the Convert class or the Parse method. For example, the following statement converts the string to a long number by using the Parse method on the Int64 class:

long num = Int64.Parse(args[0]);

It is also possible to use the C# type long, which aliases Int64:

long num = long.Parse(args[0]);

You can also use the Convert class method ToInt64 to do the same thing:

long num = Convert.ToInt64(s);