This article mainly introduces C # in the static void Main (string[] args) parameter in detail, this article through the specific examples to introduce you very detailed, the need for friends can refer to the following
The most common example program for Learning C # programming is to output Hello world! in a console application
Using System;namespace demomainargs{class Program {static void Main (string[] args) { Console.WriteLine ("Hello World ! "); Console.ReadLine (); } }}
Static: The main method is declared static, which is the entry for the application.
void: Indicates that the main method does not return any content.
String[]args: This is used to receive command-line incoming arguments. String[] is the data type that declares args and can store string arrays.
The command window pops up when you start the program with the Cmd.exe program, where you can enter parameters, string[] args refers to the arguments entered in the command window, that is, command-line arguments.
Args is used to receive command-line arguments. It is optional, not required.
The following is illustrated by a specific example program.
1. New Console program Demomainargs.
2. Write the code.
Using System;namespace demomainargs{class Program {//<summary>///Application portal//</summary>//<param Nam E= "args" > Receive command Line arguments passed in (separated by spaces between multiple parameters) </param> static void Main (string[] args) { if (args! = null) { int argslength = args. Length; Console.WriteLine ("main function parameter args length:" + argslength); for (int i = 0;i < argslength;i++) { Console.Write ("+ (i + 1) +" parameters are: "); Console.WriteLine (Args[i]); } } Console.ReadLine (); } }}
3. Press F6 to compile and generate the DemoMainArgs.exe file. Remember not F5 compile and run!!!
Here are 3 ways to show specific effects
Mode one: Start the program in the command line and enter the parameters
Open the command line, enter the path +demomainargs, empty one, and then enter a B c d (you can enter any number of parameters, separated by a space)
Enter ENTER to view the results:
、
Mode two: input parameters before compiling
Right-click on "Project"-"Properties" to popup the configuration interface. Select "Debug"-"Startup Options"-"command line arguments" text box to enter any parameters, separated by a space
Press F5 to compile and run and see the output directly
Method three: By adding parameters to the shortcut in the program
After the program compiles, create a shortcut program. Right-click on "DemoMainArgs.exe"-"Create Shortcut"
A shortcut file was generated, as follows:
Right click on "DemoMainArgs.exe Shortcut"-"Properties"-"shortcut"-"target", enter any parameters at the end of the text box,
Separated by a space between multiple
Double-click the DemoMainArgs.exe shortcut to run the results as follows
Summary: The 3rd Way is more flexible, can be the client (the user) freely input parameters within the range of features, and then the program according to the input parameters to determine the execution of different business logic.