Why does the main function in C # be static

Source: Internet
Author: User

Assuming that there is no static keyword, that means that you need to generate an instance to call the main method, and the main method is the program entry point, you do not enter the main method, you can not create an instance, since there is no instance, it can not call the main function, it is 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 particularity, 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 the executable program. C # uses a complete object-oriented approach, and C # cannot have global functions like C + +.

The Main () function must be a static function (static). This allows C # to run the program without having to create an instance object.

The Main () function protection level has no special requirements, public,protected,private and so on, but generally we have designated 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 parameters of the Main () function have only two parameter forms: a command-line argument that has no arguments and a string array, that is, the static

void main () or static void Main (String[]args), which accepts command-line arguments. There can be only one main () function entry point 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 parameters, which can cause compilation warnings.

The return value of the Main () function 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 it can be said that all instances of the class share a static variable.

Let's see what's static class, and I'm just beginning to declare an abstract sealed class

and distressed, abstract and sealed together? The purpose of defining abstract is to abstract the base class, seal represents an entity class that cannot be inherited, which is completely different, how can it be used together? Abstract indicates that it must be inherited, and sealed indicates that it cannot be inherited. These two are contradictory, how can we use them together.

By looking at the IL code, we will find that the static class is actually an abstract sealed class,

Only the compiler checks the static class's member modifiers at compile time. Let's guess what MS developers think: Their goal is to get the effect of the abstract sealed class, but they are distressed by the semantic contradictions, and we want to get a graceful solution, so we've added a static keyword for C 2.0, which perfectly solves The problem.

One might ask: why not static class = abstract class + static member limit

And to add sealed restrictions? The reason is to inherit a class with only static members and to write a class that finishes

There is no difference. Why do you say that? Because static is necessarily not virtual, what can you inherit from it?

Use it?

Static class also has a limit, that is, can only inherit from System.Object, why there is this limit

The same reason as above.

Whenever I mention the static keyword, I think of the static constructor, thanks to the MS developer for

To provide such a good feature. However, C provides a static constructor but does not provide a static destructor, but in applied Microsoft. NET Framework Programming

Jeffery Richter offers us a way to get through System.AppDomain.DomainUnload.

Events to achieve the same effect. Using static constructor It is important to note that any exception thrown here will cause the type to be no longer available before reloading the appdoamin, so be careful, and in addition, there are two types of deadlock in the static contructor. Be sure to think clearly when writing your code.

The difference between const and readonly const==static readonly

The concept of const is a variable that contains values that cannot be modified.

A constant expression is an expression that can be fully evaluated at compile-time. Therefore, constants cannot be initialized from values that are extracted from a variable. If the const int a = B+1;B is a variable, it is obvious that the result cannot be computed at compile time, so constants cannot be initialized with variables.

ReadOnly allows you to set a field to a constant, but you can perform some operations to determine its initial value.

Because ReadOnly is executed at the time of calculation, of course it can be initialized with some variables.

ReadOnly are instance members, so different instances can have different constant values, which makes readonly more flexible.

The ReadOnly keyword differs from the const keyword.

1. The const field can only be initialized in the declaration of the field.

The readonly field can be initialized in a declaration or constructor. Therefore, depending on the constructor used, the ReadOnly field may have different values.

2. The const field is a compile-time constant, and the ReadOnly field can be used to run a constant number.

3. The const default is static, and readonly must display a declaration if it is set to static.

4.const for constants of reference types, the possible values are only string and null.

ReadOnly can be of any type

----------------------------

A non-static member, also known as an instance member, must act on an instance. The instance member cannot be invoked, including the non-static Main method, when the program is just beginning to run without any instances being established. In order to be able to execute the main method at the beginning of the program, it must be declared as static.

By the way, the member called in the Main method must also be static unless the corresponding instance has been established.

For example:

Namespace Lover_p.test {

public class Test {

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

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

public static void Main () {

Instancemethod (); Error! The instance member was invoked, and no instance was created at this time

Staticmethod (); Right! You can call a static member

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

Sometest.instancemethod (); The instance member is then invoked on this instance.

Sometest.staticmethod (); It is also an error to call a static member on an instance.

}

}

}

Why does the main function in C # be static

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.