C # detailed description of the initialization sequence of members

Source: Internet
Author: User
Tags reflector


C # Do you really know the initialization sequence of the members? I found it a little pitfall, and I suddenly got a little confused about it. Next let's start the analysis and first look at three simple classes.

[Csharp]
Public abstract class Base
{
Public Base ()
{
SetValue ();
}
Public abstract void SetValue ();
}
 
Public class Sub: Base
{
Public string value;
Public Sub ()
{
Value = "chentaihan ";
}
 
Public override void SetValue ()
{
Value = "Chen taihan ";
}
}
 
Public class Sub1: Base
{
Public string value = "chentaihan ";
 
Public override void SetValue ()
{
Value = "Chen taihan ";
}
}

 

If you execute the following code, what values will be output? Please do not look down and give your own answers first.

[Csharp]
Static class Program
{
Static void Main (string [] args)
{
Sub sub = new Sub ();
Console. WriteLine (sub. value );
Sub1 sub1 = new Sub1 ();
Console. WriteLine (sub1.value );
Console. Read ();
}
}

 

Yes, he is simple, but are you sure you are right? I got a wrong answer to such a simple question, so I had this blog. Clr via c # This book tells us that initialization of a member during definition is equivalent to initialization at the top of the constructor. If a member is initialized during definition and assigned a value in the constructor, after the constructor is executed, the value of this Member is the value assigned in the UDF. Therefore, the answer is chentaihan. But this is not the answer. When the running results come out, I am confused ......

 

Let's talk about my simple analysis:

1: Enter the subclass Constructor

2: The Sub member variable memory is allocated.

3: Call the parent class Constructor

4: Call the subclass method SetValue (this method is overwritten by the subclass), and the value is assigned a value.

5: The subclass constructor is officially executed. The value of the member variable is assigned www..2cto.com again.

From the above five steps, I can see that the results are the same as those output by chentaihan. Where is the error?

So I checked it with Reflector and the result was as mentioned above, their source code is the same, as shown below. As clr via c # said in this book, why are the results different? The Reflector code is the same, but the execution results are different. What is the problem, then I can only say that Reflector is not capable of reflecting the true execution logic of the program. I have to use IL, but I am not familiar with it yet.

[Csharp]
Public class Sub: Base
{
Public string value;
Public Sub ()
{
Value = "chentaihan ";
}
 
Public override void SetValue ()
{
Value = "Chen taihan ";
}
}

 

In the case of Shenma, their IL code is different,


 

After reading this figure, we know that the answer is chentaihan and Chen taihan. Who can tell me how to call the constructor of the parent class and how to assign values differently. The tool used is used. How can I prove this result? So I started a single-step debugging and found a secret that was found every day: the member initialization was executed before the constructor. No wonder this book says that the initialization of a member during definition is equivalent to initialization at the top of the constructor, and Reflector confirms this answer. But another problem is that the constructor has not been called and the memory has not been allocated. How can I assign values to the member variables? This is not a problem. It can be seen that the assignment of member variables is only called before the constructor of the parent class. It must also be assigned to member variables after the member variables of the Child class are allocated space. Okay. The final conclusion is:

1: Enter the subclass Constructor

2: The Sub member variable memory is allocated.

3: assign values to Sub member variables

4: Call the parent class Constructor

5: Call the subclass method SetValue (this method is overwritten by the subclass), and the value is assigned a value.

6: The subclass constructor is officially executed, and the value of the member variable is assigned a value again.

The answer to this explanation is reasonable, but it also indicates that the initialization of the member variables in the definition is different from the value assigned in the constructor, at least the execution order is different, the results may be different.

 


 

Author: Chen taihan

 


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.