This article analyzes the initialization order of C# objects. Share it for your reference. The details are as follows:
using System; using ; using ; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { A a = new B(); (); } } class A { //When calling this constructor: x=1,y=0 public A() { //This function is overloaded in class B, so the output is x=1,y=0 ("In the constructor of class A"); PrintFields(); } public virtual void PrintFields() { } } class B : A { //The execution order of derived class variables/static members is better than that of base classes //First variable, so x=1,y=0 int x = 1; int y; //Post constructor, at this time, first call the base class constructor public B() { // When running here, the Class A constructor has been executed y = -1; ("In the constructor of class B"); // At this time x=1,y=-1 PrintFields(); } public override void PrintFields() { ("x={0},y={1}", x, y); } } }
Here I would like to remember the initialization order of c# objects and the initialization order of C++ objects:
C# object initialization
1. First variable and then construct the function. The variable is initialized first, and the constructor is executed
2. Static first and instantiate. When a class is accessed, the static variables and constructors are initialized first. Then the instantiation variables and constructors of the object are initialized
3. Derivate the class first and then the base class. For variables and static constructors, the derived object is initialized before the base object. For example, Class C is derived from Class B and Class B is derived from Class A, so the order in which the variables and static constructors are initialized is C-B-A.
4. Except for instance constructor. For instance constructors, the base class constructor is executed before derives the class constructor, and the instance constructor is executed in the order of A-B-C.
5. Do not assume the order of variables. Fields are initialized in sequence according to the order in which they are declared in the source file. However, since programmers and tools can arrange the declaration of variables at will, you should not initialize in any particular order in dependence on variables.
6. Use two stages to construct virtual methods. Avoid calling virtual methods from a constructor. If you need to call some virtual methods when initializing an object, you should use a two-stage construction where the object is completely constructed, and then call the initialization method of the constructed object.
C++ constructor call order
1. If there are member classes in the class, the constructor of the member class is called first;
2. Create an object of the derived class, and the constructor of the base class is called first (also preferred over the member classes in the derived class);
3. Base class constructor If there are multiple base classes, the order of call of the constructor is the order in which a certain class appears in the class derived table rather than in the member initialization table;
4. Member class object constructor If there are multiple member class objects, the order in which the constructor is called is the order in which the objects are declared in the class rather than the order in which they appear in the member initialization table;
5. Derived class constructor. As a general rule, derived class constructor should not directly assign values to a base class data member but pass the value to the appropriate base class constructor. Otherwise, the implementation of the two classes becomes tightly coupled (tightly coupled) and it will be more difficult to correctly modify or extend the base class implementation. (The responsibility of the base class designer is to provide a set of appropriate base class constructors)
I hope this article will be helpful to everyone's C# programming.