Think of by a quiz

Source: Internet
Author: User
Tags c constructor mscorlib
Csdn Community C # Someone has a basic question. I have nothing to do with it. Today I have a question like this:
(Original question link: http://community.csdn.net/Expert/topic/3918/3918948.xml? Temp =. 2294428)

Write a console applicationProgramTo complete the following functions and answer the question.
1. Create a class A, output "A" in the constructor, create a Class B, and output "B" in the constructor ".
2. inherit a new class named C from a and create a member B in C. Do not create constructors for C.
3. Create an object of class C in the main method and write the output result after running the program.
4. If a constructor is created in C to output "C", what is the result of the entire program running?

It is easy to return to this question. My answer is: 1 Using System;
2
3 Class A
4 {
5 Public A ()
6 {
7Console. writeline ("A");
8}
9 }
10
11 Class B
12 {
13 Public B ()
14 {
15Console. writeline ("B");
16}
17 }
18
19 Class C:
20 {
21 Public C ()
22 {
23Console. writeline ("C");
24}
25 B =   New B ();
26 }
27
28 Class Test
29 {
30 Public   Static   Void Main ()
31 {
32C= NewC ();
33
34}
35 }

The answers are the same as those provided by the author, but the author did not answer a question raised by someone when finishing the post: What is the output order?

I thought about it myself. First, we can see that the output solution result "B" is output by calling the constructor of Class B, and "A" is output by calling the constructor of Class, "C" calls the constructor output of class C.
Class B appears as a field in Class C, and class C inherits Class. So, I guess, to construct a C object in the main function, first initialize the C Class field, then call the constructor of the ancestor class, and then implement the C class self-ConstructorCodeIn this way, this order is explained.

So what about this conjecture?

It may be a bit confusing to initialize fields of class C first. Why do we have to initialize fields of the class first? Here we will talk about another problem: Where is the field's inline initialization? (here, inline refers to initializing and assigning values when declaring a field, such as public int I = 1, instead of public in I 《. in section 9.1 of Net Framework Program Design, the instance constructor has an explanation. Here I only say the result, that is, inline Initialization is actually completed in the Type constructor. In this case, initialize the console in Class B and C. the writeline () method occurs in the constructor of the C class. In fact, the constructor that calls the parent class A also occurs in the constructor of the C class. As for why to put the inline initialization field at the beginning, I don't know. Please give me some advice (maybe you can fix the field first and then create rules for the method, haha ). However, this order is correct, and there is Il to testify: 1 . Method Public Hidebysig specialname rtspecialname
2 Instance Void . Ctor () cel managed
3 {
4 // Code size 28 (0x1c)
5 . Maxstack 2
6 Il_0000: ldarg. 0
7 Il_0001: newobj instance Void B:. ctor ()
8 Il_0006: stfld Class B c: B
9 Il_000b: ldarg. 0
10 Il_000c: Call instance Void A:. ctor ()
11 Il_0011: ldstr " C "
12 Il_0016: Call Void [Mscorlib] system. Console: writeline ( String )
13 Il_001b: Ret
14 }   // End of method C:. ctor

The above is the Il code of the C constructor. We can clearly see that instance B can be created first, and the constructor B can be called at the same time, store B as a field of the C object, call the constructor of A, and then call the writeline method.

Some people may not understand whether B = new B () is inline initialization, so I will add a line of code int I = 1; to the end, so that the C class is changed: Class C:
{
Public C ()
{
Console. writeline ("C");
}
B =   New B ();
Int I =   1 ;
}

Then il becomes: . Method Public Hidebysig specialname rtspecialname
Instance Void . Ctor () cel managed
{
// Code size 35 (0x23)
. Maxstack 2
Il_0000: ldarg. 0
Il_0001: newobj instance Void B:. ctor ()
Il_0006: stfld Class B c: B
Il_000b: ldarg. 0
Il_000c: LDC. i4. 1
Il_000d: st1_int32 C: I
Il_0012: ldarg. 0
Il_0013: Call instance Void A:. ctor ()
Il_0018: ldstr " C "
Il_001d: Call Void [Mscorlib] system. Console: writeline ( String )
Il_0022: Ret
}   // End of method C:. ctor

You can see, this time it is clear that the initialization of the field is in the front, and it also illustrates a problem, that is, to avoid inline initialization of the field, this will increase the code size, put the initialization in the constructor.

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.