The Main () method parameter.
You can send parametersMainMethod.
static int Main(string[] args)
static void Main(string[] args)
[Note]MainTo enable the command line parameters in the method, you must manually modify the parameters in program. cs.Main. The code generated by the Windows Form Designer is created without any input parameters.Main. You can also use Environment. CommandLine or Environment. GetCommandLineArgs to access command line parameters from any location in the console or Windows application.
MainThe method parameter is a String array that represents the command line parameters. Usually pass the testLengthAttribute to determine whether a parameter exists, for example:
if (args.Length == 0) { WriteLine("Hello World."); return 1; }
You can also use the Convert class orParseMethod to convert string parameters to numeric values. For example, the following statement uses the Parse methodstringConvertlongNumber:
long num = Int64.Parse(args[0]);
You can also use the aliasInt64C # typelong:
long num = long.Parse(args[0]);
You can also useConvertClass MethodToInt64Complete the same job:
long num = Convert.ToInt64(s);
Example
The following example shows how to use command line parameters in a console application. The application uses a parameter at runtime to convert the parameter to an integer and calculate the factorial of the number. If no parameter is provided, the application sends a message to explain the correct usage of the program.
public class Functions { public static long Factorial(int n) { if ((n < 0) || (n > 20)) { return -1; } 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) { Console.WriteLine("Please enter a numeric argument."); Console.WriteLine("Usage: Factorial <num>"); return 1; } int num; bool test = int.TryParse(args[0], out num); if (test == false) { Console.WriteLine("Please enter a numeric argument."); Console.WriteLine("Usage: Factorial <num>"); return 1; } long result = Functions.Factorial(num); if (result == -1) Console.WriteLine("Input must be >= 0 and <= 20."); else Console.WriteLine("The Factorial of {0} is {1}.", num, result); return 0; } }