Subdivide first:
The members of a class are divided into: Fields, properties, methods, construction methods
Modifier for member: Static member, instance member
Hierarchy: Parent class, Child class
The inheritance relationship is not considered first, and the Order of execution is:
- Static fields
- Static Construction method
- Instance fields
- Instance Construction method
Properties and methods are executed at the time of invocation and are not considered here. How do you understand the above implementation process? If I were to design the implementation process, what should I consider, based on what?
First of all, static things are shared, that is, the same. You should care about what you share and what you do with yourself. "Against", hehe.
Second, before instantiating, you should initialize your own internal data first.
Now consider the inheritance relationship, the order of execution is:
- The static field of the child class
- Static construction methods for subclasses
- Instance field of the child class
- Static fields of the parent class
- Static construction method of parent class
- Instance fields of the parent class
- Instance construction method for parent class
- Instance construction methods for subclasses
The execution order of the parent class is added between the instance fields of the child class and the instance construction methods of the subclass. This is also very well understood: before the instance of a subclass constructs a method, you really need to know the information of the parent class, because the subclass inherits something from the parent class. This is like, no Lao Tzu, where the son, hehe.
In particular, it is important to note that not every instantiation is the order of the above. Because static members are only executed at the time of the first instantiation, they will not be executed at any later instantiation. Well understood, static members mean everyone is shared, and only this one. After the first instantiation of a static member, everyone is shared, instantiated again, and there is no need or permission to execute the part of the static member.
Additional notes:
1. When constructing an object of reference type, the memory allocated for the object is always zeroed before the instance constructor method is called, the constructor does not explicitly re-write the fields, the value of the field is 0, or null
2, in principle, the fields in the class should be initialized within the instance construction method. The C # compiler provides a simplified syntax that allows initialization when a variable is defined. But behind the scenes, C # will move this part of the code inside the construction method. Therefore, there is a problem with code bloat. Multiple fields are initialized at the time of definition, and there are multiple construction methods, each of which moves the code that initializes the fields into its own interior, which can cause the code to swell. In order to avoid this, the initialization of these fields can be put into a non-parametric construction method, the other construction methods explicitly call the non-parametric construction method.
3. There are two methods for initializing a field of a class, ① using simplified syntax, initializing at the time of definition, and ② initializing within a constructor method. Code that is initialized with simplified syntax is moved to the construction method. It is particularly important to note that in the generated IL, the parent class construction method is sandwiched between ① and ②. Therefore, when the subclass is instantiated, the ① is executed first, the parent class construction method is executed, and then the ② is executed. Now the problem is, if in the parent class constructor method, call virtual method, virtual method back to tone class method, subclass method uses field, this time the value of the field is the value of simplifying syntax initialization.
Order of execution of C # instantiation classes