C # class member initialization sequence

Source: Internet
Author: User

First, let's take a look at the initialization process of the reference type members.

Let's look at an example.

Class Program {static void Main (string [] args) {DriveB d = new DriveB ();}}

Class BaseA {static DisplayClass a = new DisplayClass ("base class static member initialization ");

DisplayClass BaseA_c = new DisplayClass ("base class instance variable BaseA_c initialization ");

Public BaseA () {Console. WriteLine ("the base class constructor is called ");}}

Class DriveB: BaseA {static DisplayClass DriveB_ B = new DisplayClass ("Inheriting class static member DriveB_ B initialization ");

// Static BaseA DriveB_a = new BaseA ();

DisplayClass DriveB_c = new DisplayClass ("Inheriting class instance variable DriveB_c initialization ");

Public DriveB () {Console. writeLine ("inherited class constructor called");} class DisplayClass {public DisplayClass (string diplayString) {Console. writeLine (diplayString); Console. the result of the WriteLine () ;}} program's dynamic row is: inheritance class static member DriveB_ B initialization inheritance class instance variable DriveB_c initialization base class static member initialization base class instance variable BaseA_c initialization base class constructor called inheritance class constructor called

The initialization sequence is as follows:

1) inherit static member variable initialization 2) inherit class instance variable initialization 3) base class static member variable initialization 4) base class instance variable initialization 5) base class constructor call 6) call the inherited class constructor.

It seems that the result is a little different from that of JAVA. It's a bit confusing. I don't know why M $ wants initialization to be executed in this order, how nice is a strict base class to a derived class like JAVA. the running result of the above example shows that the constructor is different from the execution sequence of our general ideas. for instance Member initialization, perform the following steps: class 1 object initialization in general order instance members are assigned to constructor 2 members are assigned to each other. Initialization follows the sequence from subclass to parent class 3 constructors are initialized from parent class to Child class note that, because the Member assignment Initialization is from the subclass to the parent class, do not reference the member defined by the parent class during the subclass member assignment initialization process, at this time, the parent class member has not started initialization. note that C # after the memory is allocated in the first step of object creation, all instance member variables will be initialized to the default value of the variable. For example, if the integer is 0, the reference type is null. the initialization process of member variables starts.

 

C #Object initialization

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. First, the derived class 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, then the initialization order of variables and static constructors is C-B-A.

4. Besides the instance constructor. For the instance constructor, the base class constructor is executed before the derived class constructor, And the instance constructor is executed in the order of A-B-C.

5. Do not assume the order of variables. Fields is initialized in sequence based on their declaration in the source file. However, since programmers and tools can arrange variable declarations at will, you should not initialize the variables in any special order.

6. Construct the virtual method in two phases. Avoid calling Virtual Methods From a constructor. if you need to call some virtual methods when initializing an object, you should use the two-phase building in the complete structure of the object, and then call the initialization method of the constructed object.

Related Article

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.