Further discussion on the virtual function of the constructor function call

Source: Internet
Author: User
Tags constructor count mscorlib
function on the blog to see an article "FxCop constructorsshouldnotcallbaseclassvirtualmethods norms," the original: http://blog.csdn.net/xamcsdn2/ Archive/2004/08/11/71766.aspx

Quite interested in writing a piece of code research (the estimate is still not a study, just explore it) a bit.

The code is as follows.

Using System;

Namespace Test_console
{

??? Class BaseClass
??? {
??????? public int count = 0;
??????? Public BaseClass ()
??????? {
??????????? Console.WriteLine ("Call BaseClass ' s constructor Function ...");
??????????? Increase ("base");
??????? }

??????? Public virtual void increase (string type)
??????? {
??????????? Console.WriteLine (type);
??????????? Console.WriteLine ("Call BaseClass ' s increase Function ...");
??????????? Count + +;
??????? }
??? }

??? Class Subclass:baseclass
??? {
??????? public int subcount = 0;

??????? Public Subclass ()
??????? {
??????????? Console.WriteLine ("Call subclass ' s constructor Function ...");
??????????? Console.WriteLine (Subcount. ToString ());
??????????? Increase ("sub");
??????? }

??????? public override void Increase (string type)
??????? {
??????????? Console.WriteLine (type);
??????????? Console.WriteLine ("Call subclass ' s increase Function ...");
??????????? Subcount + +;
??????????? Console.WriteLine (Subcount. ToString ());
??????? }
??????? ///
??????? The main entry point for the application.
??????? ///
??????? [STAThread]
??????? static void Main (string[] args)
??????? {
??????????? Subclass C = new subclass ();
??????????? Console.ReadLine ();
??????? }
??? }
}


The BaseClass constructor calls the increase () function, and the subclass constructor also calls this function. The results of the operation are as follows:

Call BaseClass ' s constructor Function ...
Base
Call subclass ' s increase Function ...
1
Call subclass ' s constructor Function ...
1
Sub
Call subclass ' s increase Function ...
2

You can see that subclass's increase function was called two times, and I added a parameter to the increase of subclass to determine which constructor was invoked, and the result was called by BaseClass and then by subclass. And it seems that BaseClass is called before subclass, but what is the truth? Let's take a look at the IL code for the subclass constructor:

. method public hidebysig specialname rtspecialname instance void. ctor () CIL managed
{
????? Code size:51 Byte (s)
????? . maxstack 2
????? l_0000:ldarg.0
????? l_0001:ldc.i4.0
????? L_0002:STFLD Int32 Test_console.subclass::subcount
????? l_0007:ldarg.0
????? L_0008:call instance void Test_console.baseclass::.ctor ()
????? L_000d:ldstr "Call Subclass\ ' s constructor Function ..."
????? L_0012:call void [Mscorlib]system.console::writeline (String)
????? l_0017:ldarg.0
????? L_0018:ldflda Int32 Test_console.subclass::subcount
????? L_001d:call instance string int32::tostring ()
????? L_0022:call void [Mscorlib]system.console::writeline (String)
????? l_0027:ldarg.0
????? L_0028:ldstr "Sub"
????? L_002d:callvirt instance void Test_console.baseclass::increase (String)
????? L_0032:ret
}
At a glance, the L_0008:call instance void Test_console.baseclass::.ctor () first invokes the BaseClass constructor in the subclass constructor.

?

PostScript: As the basic knowledge of the designer of the object program, it is here to talk about the subclass constructor calling the parent class constructor This seems a little superfluous, as the younger brother's first work, I hope to give those beginners some help.


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.