C # The base class constructor calls the virtual Method

Source: Internet
Author: User

Today, I found out Don Box's "Essential. Net" and the issue of virtual method allocation that I have been pondering over and over again. In. net, if the constructor of the base class calls a virtual method, it does not directly call the virtual method in the base class, instead, the virtual method of the base class is called only when the override version does not exist in the subclass. If the subclass contains the override version, the subclass method is called. Let's take a look at a small example.

Public class Fruit

{

Public
Fruit ()

{

WriteFruitName ();

}

Public virtual void
WriteFruitName ()

{

Console. WriteLine ("Fruit's WriteFruitName ()");

}

}

Public class Banana: Fruit

{

Public override void WriteFruitName ()

{

Console. WriteLine ("Banana's WriteFruitName ()");

}

}

Class Program

{

Static void Main (string []
Args)

{

Banana
B = new Banana ();

Console. ReadLine ();

}

}

}

The result after this method is executed is: Banana's WriteFruitName (); Because Banana inherits from Fruit
Banana () first calls the base class constructor Fruit (), but calls the virtual method WriteFruitName in the base class constructor. When the program runs this time, check whether the override version of The WriteFruitName method is available in the Banana method table. If so, call the method. If not, call the virtual method of the base class.

Let's take a look at a little deeper example. The truth is the same. Add a name attribute to the method,

Public class Fruit

{

Public
Fruit ()

{

FruitName = "Fruit ";

WriteFruitName ();

}

Public virtual void
WriteFruitName ()

{

Console. WriteLine ("Fruit's WriteFruitName (), The Name Is"
+ FruitName );

}

Protected
String FruitName {get; set ;}

}

Public class Banana: Fruit

{

Public
Banana ()

{

FruitName = "Banana ";

}

Public override void
WriteFruitName ()

{

Console. WriteLine ("Banana's WriteFruitName (), The Name Is"
+ FruitName );

}

}

Class Program

{

Static void Main (string []
Args)

{

Banana
B = new Banana ();

Console. ReadLine ();

}

}

The execution result of this method Is: Banana's WriteFruitName (), The Name Is Fruit. The reason is that when the virtual method is called, it is found that the subclass has replaced this method, so the WriteFruitName of the Banana class is called, but the constructor of the Banana class has not yet been executed, that is to say, the FruitName attribute has not been assigned a value in the Banana class, And the constructor of the base class assigns it a value of Fruit. This method is only for appreciation, and is rarely used in actual production.

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.