In the C # program, there is no concept of global variables. This means that only instances of this class of member variables can operate on the data, which plays the role of "information hiding. However, sometimes this is not a wise choice. Static variable in C #-It is similar to a global variable in the class and stores the public information of the class. All classes (rather than objects or instances) share the value.
Static constructor-static modified constructor. Static constructor is a new feature of C # And is not widely used in programming, it is mainly used to initialize some static variables. Because this constructor belongs to a class and does not belong to any instance, this constructor will only be executed once, and before creating the first instance of this class or referencing any static members. net Automatic Call. Static classic applications-singleton Mode
public class Singleton { private static Singleton instance; private Singleton() { } public static Singleton GetInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
1. Pay attention to the following points when using static constructors:
1. The static constructor neither has an access modifier nor a parameter.-- Because it is called by. net, modifiers such as public and private are meaningless.
2. When the first class instance is created or any static member is referenced,. Net automatically calls the static constructor to initialize the class.-- In other words, we cannot directly call static constructors, nor can we know when static constructors will be called.
3. A class can have only one static constructor.
4. A non-parameter constructor can coexist with a static constructor.-- Although the parameter list is the same, one belongs to the class and the other belongs to the instance, so there is no conflict.
5. Run only once at most.
6. Static constructors cannot be inherited.
7. If no static constructor is written and the class contains static members with an initial value, the compiler automatically generates the default static constructor.
Class initialization sequence in C:
1. Variable first and then constructor.
-- The variable is initialized first, and then the constructor is executed.
2. First static and then instantiated.
-- When a class is accessed, the static variables and constructors are initialized first. Then the object's instantiated variables and constructors are initialized.
3. Class class and base class initialization sequence
A. For variables and static constructors, first the derived class and then the base class.
-- Its derived object is initialized before the base object. For example, Class C is derived from Class B, Class B is derived from Class A, then the variable and static constructor is initialized in the order of C-B-A.
B. The opposite is true for the instance constructor. The base class is used before the derived class.
-- The base class constructor is executed before the constructor of the derived class. For example, if Class C is derived from class B and class B is derived from Class A, the execution order of the Instance constructor is A-B-C.