C # method: Virtual method call

Source: Internet
Author: User

We often encounter polymorphism issues during interviews. I have been plagued by such issues before and cannot tell which method to execute.
Let's give a simple interview question first. Let's guess, what is the output?
View Code
 
Public class
{
Public void MethodF ()
{
Console. WriteLine ("A.F ");
}
Public virtual void MethodG ()
{
Console. WriteLine ("A. G ");
}
}
Public class B:
{
New public void MethodF ()
{
Console. WriteLine ("B. F ");
}
Public override void MethodG ()
{
Console. WriteLine ("B .G ");
}
}
Class Test
{
Static void Main ()
{
B B;
B = new B ();
A a = B;
A. MethodF ();
B. MethodF ();
A. MethodG ();
B. MethodG ();
}
 
First, let's take a look at the definition of the virtual method (MSDN ):
If the declaration of an instance method contains a virtual modifier, the method is called a virtual method. If no virtual modifier exists, this method is called a non-virtual method.
Take the code in the Test class Main as an example to briefly describe what the CLR has done to create objects.
1) First, declare a reference type variable B, which is only a reference and saved on the stack of the thread for storing the valid address of object B in the future. At this time, B does not point to any valid instance. The value is null and the related code is:
B B;
 
2) Next, create the execution object through new, namely:
B = new B ();
The instance of the object is stored on the managed stack. When the CLR creates a new object, it also creates its type object (if the type object does not exist ).
The memory of the object instance in the heap includes instance fields, type object pointers, and synchronized index blocks. Type object pointers point to type objects.
The memory allocated by type objects in the heap includes instance fields, type object pointers, synchronized index blocks, static fields, and method tables.
 
3) A 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 pointed to by B.
 
4) The method is called later. The following describes how to call the method in C:
A. MethodF ();
When calling an object method, the system will directly check the type of the object variable (a), locate the type object in the heap, and check whether the method exists, if the method does not exist, you can use the type object pointer of the type object to perform a lookup until it is found, and then check whether the method is a virtual method. If it is not virtual, you can call it directly because the MethodF method is non-virtual, therefore, the output A.F is called directly.
A. MethodG ();
If this method is a virtual method with a virtual keyword, find instance Class B of the object based on object variable (, check whether the virtual method (override keyword) has been re-implemented in this type of object. If yes, run OK. If not, check its parent class up until it is found and then executed, if MethodG is a virtual method, instance B is searched. Because MethodG is rewritten in instance B, B .G is output here.

Through the above description, we should be able to easily output the first question, so we will not be so embarrassed here.
 
In general, several Keywords of virtual new override often appear in the multi-state interview questions. The new Keyword implements a new method and hides the Same Name method of the base class.

 


From Nirvana 2012

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.