Detailed explanation of static void Main(string[] args) parameters in C#

 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: is to 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 that declares 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.

  using System;
  
  namespace DemoMainArgs
  {
      class Program
      {
          /// <summary>
          /// 应用程序入口
          /// </summary>
         /// <param name="args">接收命令行传入的参数(多个参数之间用空格分开)</param>
         static void Main(string[] args)
         {
             if(args != null)
             {
                 int argsLength = args.Length;
                 Console.WriteLine("Main函数参数args的长度:" + argsLength);
                 for(int i = 0;i < argsLength;i++)
                 {
                     Console.Write("第" + (i + 1) + "个参数是:");
                     Console.WriteLine(args[i]);
                 }
             }
 
             Console.ReadLine();
         }
     }
 }

 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 one blank, 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 parameters in the text box, separated by spaces

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

 

 Method 3: By adding parameters in the shortcut of the program

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 the parameters within the characteristic range, and then the program judges and executes different business logic according to the input parameters.

Guess you like

Origin blog.csdn.net/Dust_Evc/article/details/114846520