The Main () method parameter.

Source: Internet
Author: User

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;        }    }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.