C # object creation process

Source: Internet
Author: User

First lookCode:

 Namespace Temp { Class  Program { Static void Main ( String [] ARGs ){ Class1 C = New  Class1 ();}} Class  Baseclass { Int Z = 3; Public Baseclass () {methoda ();} Public Virtual void Methoda (){ Console . Writeline ( "Baseclass. methoda" );}} Class  Class1 : Baseclass { Int X = 1; Int Y; Public Class1 () {Y = 2 ;} Public override void Methoda (){Console . Writeline (x + y );}}}

The above is a simple inheritance hierarchy. Do not use vs for testing. Let's analyze what is finally output?

If you have any questions during the analysis, please test it right away and set a breakpoint for One-Step tracking in the main method.

The entire process of one-step debugging is described here:

    1. When the yellow cursor enters the class1 class, it jumps into the first sentence.IntX = 1;
    2. The yellow cursor skips the second sentenceIntY points to the class1 constructor;
    3. Jump into the parent class before executing the constructor code block. The yellow cursor points to the parent class baseclassIntZ = 3 statement;
    4. The yellow cursor points to the baseclass constructor;
    5. The yellow cursor points to the methoda () call in the constructor;
    6. Method methoda () to override the yellow cursor to the subclass class1 ();
    7. Check the two fields and find x = 1, y = 0;
    8. The yellow cursor points to the console statement;
    9. The yellow cursor jumps out of the methoda () call of the parent constructor;
    10. The yellow cursor jumps out of the parent class constructor and points to the sub-class constructor again to execute the code block;
    11. Until the execution is completed.

 

Several sequence problems are described here:

    1. The procedure for directly assigning values to fields is executed before the constructor, and the assignment of subclass fields is prior to the assignment of fields in the parent class;
    2. The steps for field memory allocation and initialization are before the yellow cursor we can see enters the class1 class;
    3. The constructor of the parent class is executed before the constructor is executed;
    4. When the constructor is executed, the CLR can recognize method overwriting, indicating that the method loading process is prior to the field assignment step;
    5. The Int type field has been assigned a value of 0 by default during memory allocation and initialization (only int in the field, not the int variable in the method ).

Summary: when the new statement is executed, the following things occur (more details will not be discussed here ):

    1. Allocate and initialize the memory for the field;
    2. If the class is loaded for the first time (that is, other objects of the class have never been created in the system, or all GC objects have been created but are not referenced), copy the instance method to the method table;
    3. Assign values to fields that need to be directly assigned to a class;
    4. Execute the constructor.

This section only analyzes the case where the field type is int. You are welcome to explore the case of string or other reference types by yourself. Thank you for your advice.

PS: Do you have any special terms for the yellow cursor during single-step debugging in?

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.