Understanding the newest: differences between new and override and differences between virtual and abstract methods

Source: Internet
Author: User

ViewCodeRight:

 
Using system; namespace consoleapplication1 {class program {static void main (string [] ARGs) {B = new B (); B. method1 (); console. readline () ;}} Class A {Public Virtual void Method1 () {console. writeline (". method1 ") ;}} Class B: A {public void Method1 () {console. writeline ("B. method1 ");}}}

This code is simple: B inherits a and defines the method Method1 with the same name in A. The compilation is successful, but a warning is displayed:

'Leleapplication1. b. method1 () 'hides inherited member' consoleapplication1. a. method1 ()'. to make the current member override that implementation, add the override keyword. otherwise Add the new keyword.

That is: B. Method1 hides the Method1 method inherited from a. to redefine the implementation of this method, add the override keyword. Otherwise, add the new keyword.

Use reflector to view the final il code defined by Method1 in B:

If we want to eliminate this compilation warning, we can try to add a new keyword before, that is,:

 
Class B: A {New Public void Method1 () {console. writeline ("B. Method1 --> New ");}}

Let's take a look at the Il code at this time:

By comparison, it is found that there is no difference with the addition of new, that is, after the compiler finds a method definition with the same name, it is processed as a New Keyword by default in this example.

If you replace new with the override keyword, that is:

 
Class B: A {override public void Method1 () {console. writeline ("B. Method1 --> override ");}}

The definition of Il is as follows:

We can find that there is one more virtual, that is, after override is added, the compiler treats Method1 in Class B as a virtual method.

Summary:

In this example, if the developer intends to make Method1 in B generate a different processing result from Method1 IN A, no matter whether you add new, or add any one of new and override, and the running results remain unchanged. During Internal compilation, the override keyword turns the method of the same name into a virtual method, but its semantics is different.

1.Without a keyword, the code will become less readable. Suppose A and B are encapsulated intoProgramSet, and then hand it to the programmer of another company for secondary development, and the programmer behind it gets another C Class inherited from B. Will he be confused when calling B. Method1? (Although compilation will not go wrong)
2.After the New Keyword is added, the compiler is clearly told that Method1 in Class B has nothing to do with Method1 in Class A. Everyone goes through different bridges and walks through different paths.
3.After the override keyword is added, it indicates that Method1 in Class B basically recognizes the Method1 Processing Method in Class A, but it may feel that the function needs to be extended or modified, in addition, the subclass of B can continue to expand Method1 in B, and C # specifies that only the virtual method can be overwritten. Therefore, in the final compilation, method1 in B will also be compiled into a virtual method. For this, you can verify it using the following code:

 
Class B: A {New Public void Method1 () {base. method1 (); console. writeline ("B. method1 --> New ") ;}} Class C: B {override public void Method1 () {console. writeline ("C. method1 ");}}

 

If you want the C class to continue to rewrite (extend) The Method1 method in B, this will not be able to be compiled, and the prompt will be: only the method with the virtual, abstract, override keyword added, can be override! In this case, you can only replace the New in front of Method1 in B with override.

 

Let's take a look at the similarities and differences between abstract and Virtual Methods:

Read a piece of code

 
Abstract class A {Public Virtual void virtualmethod () {console. writeline ("A. virtualmethod1");} public abstract void abstractmethod ();}

First, the abstract method can only appear in the abstract class. That is, as long as there is an abstract method in the definition of a class, the class must also be an abstract class.

Secondly, virtual can have the implementation code of the method body, while abstract can only define the method signature (that is, abstract is the same as the method in the interface, only the method is defined, not the implementation method)

In the C # compiler, abstract methods are also processed as virtual methods, but abstract keywords are added.

In, we can see that abstractmethod is also marked as a virtual method. In addition, there are some differences between them in subclass inheritance. See the following code:

Class B: A {public void virtualmethod () {}/// or // override public void virtualmethod () // {//} // or // New Public void virtualmethod () // {//} public override void abstractmethod (){}}

 

That is, the abstract method in the parent class, the subclass must be implemented, and the override keyword must be used for annotation. The virtual method in the parent class can be redefined (new ), you can also overload or ignore (that is, do not define a method with the same name as the parent virtual method)

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.