C # member Initialization is a bit difficult

Source: Internet
Author: User

C #Are you sure you know the initialization sequence of the members? I found it a little pitfall, And it was suddenly a little confused about what the situation was. Next let's start the analysis first.3Simple class.

 

     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 sectionCodeWhat values will be output? Please don't look down and give your own answers first.

 

 Static ClassProgram {Static VoidMain (String[] ARGs) {sub=NewSub (); console. writeline (sub. Value); sub1 sub1=NewSub1 (); 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 to 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. I am confused when the running results come out.......

 

Let's talk about my simple analysis:

1: Enter the subclass Constructor

2:SubThe memory of the member variable is allocated.

3: Call the parent class constructor.

4: Call the subclass MethodSetvalue(Subclass overwrites this method),ValueAssigned Value

5: Formally execute the subclass constructor, member variableValueAssigned again

From above5Step by step, I find that the output results are the same.Chentaihan. Where is the error?

so I used reflector check the results and their source code is the same as described above, as shown below. Just as CLR via C # as this book says, why are the results different? reflector the code is the same, the execution results are different. What is the problem? I can only say reflector fudao, it does not reflect the true execution logic of the Program , IL I am not familiar with it yet.

  Public   class   Sub: Base {  Public   string   value;   Public   sub () {value  =  "  chentaihan  "   ;}   Public   override   void   setvalue () {value  =  "  Chen taihan  "   ;}}  

 

Shenma situation, theirIlThe code is different,

After reading this figure, we know the answer is:Chentaihan,Chen taihan. Who can tell me how to call the constructor of the parent class andValueThe order of values is different. The tools used are used. How can I prove this result? So I started a single-step debugging and found a secret I found every day: The member Initialization is executed before the constructor. No wonder this book says Initialization of a member during definition is equivalent to initialization at the top of the constructor,ReflectorThis answer is also confirmed. But another problem is that the constructor has not been called. The memory has not been allocated. How do 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:SubThe memory of the member variable is allocated.

3:IsSubMember variable assignment

4: Call the parent class constructor.

5: Call the subclass MethodSetvalue(Subclass overwrites this method),ValueAssigned Value

6: Formally execute the subclass constructor, member variableValueAssigned again

If you agree to the above points, please let me go and leave a message. If you do not agree, please leave a message.

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

Blog:Http://www.cnblogs.com/hlxs/

 

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.