Measure the test taker's knowledge about how the New Keyword blocks inheritance.

Source: Internet
Author: User

Classic. net interview questions, there are often surveys on the New Keyword, which will certainly ask what is the use of the new keyword before the method, and so on, generally, students can say that they are blocking inheritance or indicating that the method with the same name as the parent class is independent. However, when I further asked why or talked about the principle, most of them still couldn't answer. The following is a brief analysis of the implementation principles that I personally understand.

From a classic interviewCodeStart with the question and have the following questions (it is estimated that they will know the answer at a Glance)

 

View code

1 Public Class Book
2 {
3 Public Virtual String Getdescription ()
4 {
5 Return " A normal book " ;
6 }
7 }
8
9 Public Class Aspnetbook: Book
10 {
11 Public New String Getdescription ()
12 {
13 Return " An ASPnet book " ;
14 }
15 }
16
17 Public Class Wcfbook: Book
18 {
19 Public Override String Getdescription ()
20 {
21 Return " An WCF book " ;
22 }
23 }
24
25 Class Program
26 {
27
28 Static Void Main ( String [] ARGs)
29 {
30 Book B = Null ;
31
32 Aspnetbook A = New Aspnetbook ();
33 Console. writeline (A. getdescription (); // callvirt instance string callmethodtest. aspnetbook: getdescription ()
34
35 B =;
36 Console. writeline (B. getdescription (); // callvirt instance string callmethodtest. Book: getdescription ()
37
38 Wcfbook c = New Wcfbook ();
39 Console. writeline (C. getdescription (); // callvirt instance string callmethodtest. Book: getdescription ()
40
41 B = C;
42 Console. writeline (B. getdescription (); // callvirt instance string callmethodtest. Book: getdescription ()
43
44 Console. Readline ();
45 }
46 }

 

The output result is:

An ASPnet book

A normal book

A WCF book

A WCF book

Start with analyzing Il:

The annotation next to the Call Code marked in red is the corresponding il code. The obvious difference is that when you call the method of the aspnetbook instance modified by the new keyword, It is the aspnetbook method directly, instead, the method of the parent class is not found. When the aspnetbook method is called through the parent class method, call the method from the parent class, and IL gives the callvirt method, which is used to call the method that inherits polymorphism. Callvirt goes back and finds the final implementation method. When the keyword new is encountered, it stops searching, that is, it directly calls the method of the parent class book.

For a wcfbook instance that has overwritten the parent class method, the generated il code is consistent whether it is called directly through the instance or through the parent class method, the generated Il is called through the callvirt method. First, the method of the parent class is searched to find the final implementation method.

That is to say, the generation of the above il Code depends on the implementation of the Code and is understood for the moment.

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.