Versioning with the Override and New Keywords (C # Programming Guide)

Source: Internet
Author: User
Original address: Click to open the link



This has many meanings. For example, this means that introducing a new member in a base class with the same name as a member in a derived class is fully supported in C # and does not cause unexpected behavior. It also means that a class must explicitly declare whether a method is overriding an inherited method, or a new method that hides an inherited method with a similar name.

In C #, a derived class can contain a method with the same name as a base class method.

  • The base class method must be defined as virtual.

  • If a method in a derived class does not have a new or override keyword in front of it, the compiler will issue a warning that the method will perform as if the new keyword exists.

  • If a method in a derived class is preceded by a new keyword, the method is defined as independent of the method in the base class.

  • If the method in the derived class is preceded by the override keyword, the object of the derived class will call the method instead of calling the base class method.

  • The base class method can be called from a derived class using the base keyword.

  • The override ,virtual , and new keywords can also be used in properties, indexers, and events.

If a method is declared as a virtual method, any class that inherits the method can implement its own version. virtual; The modifier. Then, the derived class can use override keyword overriding base virtual method , or use new keyword hides the virtual method in the base class. override keyword and new The keyword is not specified, the compiler issues a warning, and methods in the derived class will hide the methods in the base class.

The graphicsclass is as follows:

C#



Class graphicsclass{public    virtual void DrawLine () {} public    virtual void Drawpoint () {}}


Your company uses this class, and you use it to derive your own class when you add a new method:

C#



Class yourderivedgraphicsclass:graphicsclass{public    void DrawRectangle () {}}


Your application runs fine until company a releases a new version of Graphicsclass , similar to the following code:

C#




Class graphicsclass{public virtual    void DrawLine () {} public    virtual void Drawpoint () {} public    virtual Voi D DrawRectangle () {}}


At first, no problems occurred. The new version remains binary compatible with the old version. Any software that has been deployed will continue to work, even if the new class is installed on the computer system on which the software resides. in your derived class, any existing calls to the method DrawRectangle will continue to reference your version.

This warning prompts you to consider how you want the DrawRectangle method to work in your application.

If you want your method to override the new base class method, use the override keyword:

C#



Class yourderivedgraphicsclass:graphicsclass{public    override void DrawRectangle () {}}


objects derived from Yourderivedgraphicsclass can still access the base class version of DrawRectangle using the base keyword

C#


Base. DrawRectangle ();


To avoid confusion between the two methods, you can rename your method. This can be time-consuming and error-prone, and in some cases is not feasible. However, if your project is relatively small, you can use the refactoring options in Visual Studio to rename the method. For more information, see Refactoring Classes and types (Class Designer).

Alternatively, you can prevent the warning by using the keyword new in the derived class definition:

C#


Class yourderivedgraphicsclass:graphicsclass{public    new void DrawRectangle () {}}


This is the default behavior.

overrides and method selection

The following methods will be compatible:

C#


public class derived:base{public    override void DoWork (int. param) {} public    void DoWork (double param) {}}


The overriding method is not considered to be a declaration on a class, but rather a new implementation of a method declared on a base class. only if the C # compiler cannot match a method call to the original method on Derived , it attempts to match the call to an overriding method with the same name and compatible parameters. For example:

C#



int val = 5;derived d = new Derived ();d. DoWork (val);  Calls DoWork (double).


first, avoid declaring the new method with the same name as the virtual method. second, by derived is cast to base to make C # The compiler searches the list of base class methods, making it call a virtual method. because it is a virtual method, derived The implementation of the DoWork (int) . For example:

C#


(Base) d). DoWork (val);  Calls DoWork (int) on Derived.


For more examples of new and override , see Understanding when to use the Override and New Keywords (C # Programming Guide).

The above is the content of version control using the Override and New Keywords (C # Programming Guide), more about topic.alibabacloud.com (www.php.cn)!

  • 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.