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