The invocation of the virtual method of C # Method A brief introduction to Kdalan's blog

Source: Internet
Author: User

We often encounter the problem of polymorphism in the interview, I have been plagued by such problems, I am not clear which method to execute.

First give a simple face test, everyone guess, the output is?

View CodePublicClass A
{
Publicvoid Methodf ()
{
Console.WriteLine ("A.f");
}
PublicVirtualvoid Methodg ()
{
Console.WriteLine ("A.g");
}
}
PublicClass B:a
{
NewPublicvoid Methodf ()
{
Console.WriteLine ("B.f");
}
PublicOverridevoid Methodg ()
{
Console.WriteLine ("B.g");
}
}
Class Test
{
static void Main ()
{
b b;
b = new B ();
A = b;
A.METHODF ();
B.METHODF ();
A.METHODG ();
B.METHODG ();
}

First look at the definition of virtual methods (MSDN):

  If the declaration of an instance method contains the virtual modifier, the method is called a dummy method. If there is no virtual modifier, the method is called a non-dummy method.

Take the code in the above topic, test class Main, as an example, and simply say what the CLR did to create the object.

1) First, declare a reference-type variable B, which is only a reference, which is stored on the thread's stack and is used to hold the valid address of the B object in the future. At this point, B does not point to any valid instance, the value is null, and the relevant code is:

b b;

2) Next, the creation of the object is performed by new, namely:

b =new B ();object is saved on the managed heap, and the CLR creates a new object while creating its type Object(If the type object does not exist).

  The memory of an object instance in the heap includes an instance field, a type object pointer, a synchronous index block, and a type object pointer to a type object.

  The memory that the type Object allocates in the heap includes a type object pointer, a synchronous index block, a static field, and a method table.

3) a = B; This line of code first declares a reference-type variable A of type A, and points its actual address to the object instance to which B is pointing.

4) followed by the invocation of the method, the following details the invocation of a method in C #:

A.METHODF ();

When a method of an object is called, the type of the object variable (a) is checked directly, the type object in the heap is found, the method is looked up, no, the type object pointer is traced up through the type object until it is found, and then the method is checked for virtual method, if non-virtual, directly called, because METHODF Method is non-virtual, so the output A.F is called directly.

A.METHODG ();

If the method is a virtual method, that is, the virtual keyword, according to the object variable (a), to find the object's instance Class B, find whether the type object is re-implemented in the virtual method (override keyword), if yes, OK to execute, if not, Check the parent class up until it is found and executed, METHODG is the virtual method, and the instance B is looked up, because METHODG is overridden in B, the output B.G here.


From the above description, the beginning of the interview problem, we should easily be able to produce output, here is not verbose.

General Test multi-state interview problem Virtual new override several keywords often appear, the newkeyword implementation of a novel method, while hiding the base class of the same name method.

The invocation of the virtual method of C # Method A brief introduction to Kdalan's blog

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.