C# console program entry function Main(string[] args) parameter details

C# console program entry function Main(string[] args) parameter details

 The most common example program for learning C# programming is to output Hello World in a console application!

 1 using System;
 2 
 3 namespace DemoMainArgs
 4 {
 5 class Program
 6 {
 7 static void Main(string[] args)
 8 {
 9 Console.WriteLine("Hello World!");
10 Console.ReadLine();
11}
12}
13}
  • static: Declare the Main method as static, which is the entry point of the application.
  • void: Indicates that the main method will not return anything.
  • string[]args: This is used to receive the parameters passed in from the command line. string[] is the data type declared args, which can store an array of strings.

When the above program is started through cmd.exe program, a command window will pop up, you can enter some parameters there, string[] args refers to the parameters entered in the command window, that is, command line parameters.

args is used to receive command line parameters. It is optional and not required.

The following is a specific example program to illustrate.

1. Create a new console program DemoMainArgs.

2. Write the code.

 1 using System;
 2 
 3 namespace DemoMainArgs
 4 {
 5 class Program
 6 {
 7///<summary>
 8///Application entry
 9///</summary>
10///<param name="args">Receive the parameters passed in from the command line (multiple parameters are separated by spaces)</param>
11 static void Main(string[] args)
12 {
13 if(args != null)
14 {
15 int argsLength = args.Length;
16 Console.WriteLine("The length of Main function parameter args:" + argsLength);
17 for(int i = 0;i <argsLength;i++)
18 {
19 Console.Write("第" + (i + 1) + "The parameter is:");
20 Console.WriteLine(args[i]);
21 }
22 }
23 
24 Console.ReadLine();
25}
26}
27}

3. Press F6 to compile and generate the DemoMainArgs.exe file. Remember not to compile and run with F5! ! !

The following 3 ways to show the specific effect

Method 1: Start the program on the command line and enter the parameters

Open the command line, enter the path +DemoMainArgs, leave a space, and then enter abcd (you can enter any number of parameters, separated by spaces)

Enter to view the results:

,

Method 2: Input parameters before compiling

Right-click [Project]--"[Properties], the configuration interface will pop up. Select [Debugging]--"[Startup Options]--"[Command Line Parameters] input any parameter in the text box, separated by spaces

Press F5 to compile and run, and see the output directly

Method 3: Add parameters in the shortcut of the program [recommended]

After the program is compiled, create a shortcut program. Right-click [DemoMainArgs.exe]-"[Create Shortcut]

The shortcut file is generated as follows:

Right-click [DemoMainArgs.exe shortcut]--"[Properties]--"[Shortcut]--"[Target], enter any parameters at the end of the text box,

Use spaces to separate multiple

Double-click [DemoMainArgs.exe shortcut], the running result is as follows

Summary: The third method is more flexible. The client (user) can freely input parameters within the characteristic range, and then the program judges and executes different business logics according to the input parameters.

Reference: https://cloud.tencent.com/developer/article/1507934 C# console program entry function Main(string[] args) parameter details-Cloud + Community-Tencent Cloud