Functions and explanations of static void Main (string [] args) in C #

Source: Internet
Author: User

Functions and explanations of static void Main (string [] args) in C #

Functions and explanations of static void Main (string [] args) in C #
Static indicates that the method is static. That is to say, the method is allocated memory when the program is compiled. When using the method, you do not need to generate a certain type of object and release it only when the program exits.
Void indicates that the method does not return a value, that is, the method does not have the urn keyword.
Main is the method name. Of course, this method is a special method. Main () is the main function, which is the entrance of the entire program. After the program is compiled, it is executed from here, this is also the reason why the main method is static, because this function must exist when nothing is done.
String [] args indicates that the command line parameter is a string array, that is, you are in the command line (black box) the input parameters in can make multiple strings (in a sense, everything can be a command line parameter ).
The command window will pop up during running, where you can enter some parameters. string [] args refers to the parameters you enter in the Command window. Args is used to process command line parameters. The command line parameter is the parameter that you pass to the program when running the program. It is optional and not required.

How to pass parameters to the C # Main function? We can see that the C # Main function has a string [] args parameter, so do you know what practical functions it has? Let's talk about it in detail:
C # In the console application, we often see that the Main function carries a string [] args parameter, so do you know what it is useful?
Step 1: Create a console application named Main. cs
Step 2: paste the following code.
Using System;

Class Program {

Static void Main (string [] args ){

Int argsLength = args. Length;

Console. WriteLine ("Main function parameter args length:" + argsLength. ToString ());

For (int I = 0; I <argsLength; I ++)
{
Console. Write ("no." + I. ToString () + "bit :");

Console. WriteLine (args [I]. ToString ());
}

}
}

Step 3: Compile and run main.csto generate the main.exe File

Step 4: Enter c: \> Main a B c in command line mode, and press enter to view the result.

Output result:

Length of the Main function parameter args: 3

The fourth digit is:

The 1st bits are: B

The 2nd bits are: c

Because the number of parameters is unknown, you can enter any number of parameters without entering them.

Related Article

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.