Resolve the question C # About the instantiation Sequence

Source: Internet
Author: User

The questions are as follows:
Public class BaseA
{
Public static MyTest a1 = new MyTest ("a1 ");
Public MyTest a2 = new MyTest ("a2 ");
Static BaseA ()
{
MyTest a3 = new MyTest ("a3 ");
}
Public BaseA ()
{
MyTest a4 = new MyTest ("a4 ");
}
Public virtual void MyFun ()
{
MyTest a5 = new MyTest ("a5 ");
}
}
Public class BaseB: BaseA
{
Public static MyTest b1 = new MyTest ("b1 ");
Public MyTest b2 = new MyTest ("b2 ");
Static BaseB ()
{
MyTest b3 = new MyTest ("b3 ");
}
Public BaseB ()
{
MyTest b4 = new MyTest ("b4 ");
}
Public new void MyFun ()
{
MyTest b5 = new MyTest ("b5 ");
}
}
Static class Program
{
Static void Main ()
{
BaseB baseb = new BaseB ();
Baseb. MyFun ();
}
}
Public class MyTest
{
Public MyTest (string info)
{
Console. WriteLine (info );
}
}
The final problem is: please write out the order of the ten class instantiation in the Main () method, a1-a5, and b1-b5. (I added the MyTest class to view the results. The original question is to instantiate an object class .)
I don't know how many people in the garden can write the correct answer with confidence. I am wrong. The correct answer is:
B1
B3
B2
A1
A3
A2
A4
B4
B5
Knowledge points involved in the question
Although the questions are not correct, I need to know why I did something wrong, so that I can improve. During the Dragon Boat Festival holiday, I sorted out all the knowledge points involved in this interview question. The points are as follows:
Initialize fields in inline mode.
The execution time of the Type constructor (static constructor.
C # sequence of class and subclass instantiation.
The role of the new modifier.
Inline initialization Field
This point is mentioned in CLR via C #. The so-called inline method is a simplified syntax for initializing fields. Let's look at the sample code:
Public class SomeType
{
Public int m_x = 5;
}
This method of assigning values when declaring variables in the class is called inline, which is roughly equivalent to the following code:
Public class SomeType
{
Public int m_x;
Public SomeType ()
{
M_x = 5;
}
}
The reason is "roughly equivalent", because the execution sequence of the two is slightly different, the compiler will first generate the Inline code and then call the constructor.
For example, in the following code, the final result of m_x is 10.
Public class SomeType
{
// Execute
Public int m_x = 5;
Public SomeType ()
{
// Executed later
M_x = 10;
}
}
Type constructor execution
The so-called Type constructor is a well-known static constructor. In the classes we write, there will be a default static non-argument constructor, the constructor is the same as the constructor without parameters.
Every time we create the first instance for a class or access a static field, the JIT compiler calls the static constructor of the class. Of course, static variables can also be assigned values using the inline method described above.
It can be seen that when a class is instantiated for the first time, the static constructor of the class is called first.
C # sequence of class and subclass instantiation
This knowledge point is relatively simple, that is, the base class instance constructor will be called before the subclass instance constructor is called. From the results of the interview questions, we can see that the construction method of the base class is later than the static constructor of the subclass. Due to the limited personal ability, I cannot analyze the principle from a more underlying perspective, you can only remember it for the moment.
New Modifier
I have read a lot of questions about the use of new in method declaration in the form of modifiers. The usage of new is also found on MSDN, the official saying is "explicitly hiding members inherited from the base class ".
My personal understanding is relatively simple: In a subclass, a method signature (referred to as a parameter, method name, and return value) is the same as a method of the base class. By adding a new modifier, this method can be used without changing the subclass.
After all, the new modifier allows two unrelated methods of the same name to exist at the same time. (The same name indicates the same method signature)
Last
Looking back, in fact, in my daily project development, these problems are more or less involved, but I do not pay attention to them at ordinary times and do not thoroughly understand them. writing this article also encourages myself to pay attention to the basics in the future, not impetuous.

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.