Why does the main () function in C # have to be static? __ function

Source: Internet
Author: User

Assuming there is no static keyword, that means that you need to generate an instance before you can call this main method, and the main method is the program entry point, you do not enter the main method, naturally cannot generate an instance, since there is no instance, it cannot invoke the main function, not contradictory? So the main function is set to static.

The Main () function is very special in C #, which is the entry point for all executable programs specified by the compiler. Because of its specificity, we have the following guidelines for the main () function:

The Main () function must be encapsulated in a class or struct to provide an entry point for an executable program. C # has a full object-oriented programming approach, and C # can not have global functions like C + +.

The Main () function must be a static function. This allows C # to run programs without creating instance objects.

The Main () function protection level has no special requirements, public,protected,private, and so on, but generally we all specify it as public.

The first letter of the Main () function name is capitalized, otherwise it will not have the semantics of the entry point. C # is a case sensitive language.

The argument for the Main () function has only two parameters: a command-line argument that is not a parameter and a string array, that is, a static

void main () or static void Main (String[]args), which takes command line arguments. There can be only one entry point for a main () function in a C # program. Other forms of parameters do not have entry point semantics, and C # does not recommend overloading the main () function with other parameter forms, which can cause compilation warnings.

The Main () function return value can only be void (no type) or int (integer type). Other forms of return values do not have entry-point semantics.

In C #, a static variable indicates that the variable belongs to a class, not an instance of the class. The "static" modifier declares a static element that belongs to the type itself rather than the specified object, and can be said to share a static variable with all instances of the class.

The members that are invoked in the main method must also be static, unless the corresponding instance is established.

For example:

namespace Lover_p.test {public

class Test {public

void Instancemethod () {}//instance member (non-static)

Publ IC static void Staticmethod {}//type member (static) public

static void Main () {

instancemethod ()////error! Called instance member, which was not built at this time Instance

Staticmethod (); You can call static member

Test sometest = new test ();//Create an instance of this type

sometest.instancemethod ();  It is also wrong to invoke instance members on this instance to

Sometest.staticmethod ();//Append a sentence to invoke static members on the instance!

}

}


Original link: http://net5x.blog.51cto.com/7900145/1568257%20

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.