Console app arguments, how arguments are passed to Main method

前端 未结 10 1202
醉话见心
醉话见心 2020-12-08 09:30

This would be question from c# beginner. When I create console application I get Main method with parameter args as array string. I do not understand how t

10条回答
  •  不知归路
    2020-12-08 10:11

    How is main called?

    When you are using the console application template the code will be compiled requiring a method called Main in the startup object as Main is market as entry point to the application.

    By default no startup object is specified in the project propery settings and the Program class will be used by default. You can change this in the project property under the "Build" tab if you wish.

    Keep in mind that which ever object you assign to be the startup object must have a method named Main in it.

    How are args passed to main method

    The accepted format is MyConsoleApp.exe value01 value02 etc...

    The application assigns each value after each space into a separate element of the parameter array.

    Thus, MyConsoleApp.exe value01 value02 will mean your args paramter has 2 elements:

    [0] = "value01"
    
    [1] = "value02"
    

    How you parse the input values and use them is up to you.

    Hope this helped.

    Additional Reading:

    Creating Console Applications (Visual C#)

    Command-Line Arguments (C# Programming Guide)

提交回复
热议问题