Order of execution of C # instantiation classes

Source: Internet
Author: User

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:

    1. Static fields
    2. Static Construction method
    3. Instance fields
    4. 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:

    1. The static field of the child class
    2. Static construction methods for subclasses
    3. Instance field of the child class
    4. Static fields of the parent class
    5. Static construction method of parent class
    6. Instance fields of the parent class
    7. Instance construction method for parent class
    8. 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

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.