Role and interpretation of static void Main (string[] args) in C #

Source: Internet
Author: User

Role and interpretation of static void Main (string[] args) in C #
Static means that the method is statically, which means that the method is allocated memory when the program is compiled, and does not need to generate a type of object when it is used, knowing that the program exits before it is released.
void indicates that the method has no return value, that is, the method has no Renturn keyword.
Main is the method name, of course, this method is a special method, is main () is said to be the main function, is the entire program's entrance, the program is compiled from here to execute, this is the main method if static reason, because when nothing is done, this function must exist.
String[] args means that the command line argument is an array of strings, which means that the arguments you enter in the command line (the black box) can make multiple strings (meaning that anything can be a command-line argument in a way).
The runtime will pop up a command window where you can enter some parameters, string[] args refers to the parameters you entered in the Command window. Args is used to handle command-line arguments. A command-line argument is a parameter that you pass to it when you run the program. It is optional, not required.

How do I pass arguments to the C # main function? We see the C # main function with a string[] args parameter, so do you know what the actual function is? Let's talk about it in detail below:
C # Console application, we often see the main function with a string[] args parameter, so you know what it's useful for?
Step 1: Build 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 ("First" + i.tostring () + "bit is:");

Console.WriteLine (Args[i]. ToString ());
}

}
}

Step 3: Compile and run Main.cs, generate the Main.exe file

Step 4: Enter C:\>main a b C in the command line mode and hit enter to see the results

The output is:

Main function argument length of args: 3

The No. 0 bit is: a

The 1th bit is: b

The 2nd bit is: C

Because the number of parameters is unknown, it is allowed to enter as many or as no input.

Role and interpretation of static void Main (string[] args) in C #

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.