C # beginner-use the Override and New keywords for version control (C # programming guide)

Source: Internet
Author: User

The C # language is specially designed so that version control between the base classes and derived classes in different libraries can continue to develop and maintain backward compatibility. This has many meanings. For example, this means that introducing a new member with the same name as a member in the derived class in the base class is fully supported in C # and will not cause unexpected behavior. It also means that the class must explicitly declare whether a method needs to override an inheritance method or a new method that hides an inheritance method with similar names.

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

  • The base class method must be defined as virtual.

  • If the method in the derived class does not have the new or override keyword before it, the compiler will issue a warning that the method will execute the operation like there is a new keyword.

  • If the method in the derived class has a new keyword before it, the method is defined as a method independent from the method in the base class.

  • If the method in the derived class has the override keyword before it, the object of the derived class will call this method instead of the base class method.

  • You can use the base keyword in the derived class to call the base class method.

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

By default, the C # method is non-virtual. If a method is declared as a virtual method, any class that inherits the method can implement its own version. To make a method a virtual method, you must use the virtual modifier in the method declaration of the base class. Then, the derived class can use the override keyword to override the basic virtual method, or use the new keyword to hide the virtual method in the basic class. If the override keyword and new Keyword are not specified, the compiler will issue a warning and the methods in the derived class will hide the methods in the base class. For more information, see compiler warning (level 2) CS0108.

To demonstrate the above situation in practice, we assume that Company A has created A class named GraphicsClass, and your program will use this class. GraphicsClass is as follows:

 
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 adding a new method:

 
class YourDerivedGraphicsClass : GraphicsClass
{
public void DrawRectangle() { }
}

Your application runs normally until Company A releases A new version of GraphicsClass, similar to the following code:

 
class GraphicsClass
{
public virtual void DrawLine() { }
public virtual void DrawPoint() { }
public virtual void DrawRectangle() { }
}
 

Now, the new version of GraphicsClass contains a method named DrawRectangle. At the beginning, there were no problems. The new version is still Binary compatible with the old version. Any deployed software will continue to work, even if the new class has been installed on the computer system where the software is located. In your derived class, any existing call to the method DrawRectangle will continue to reference your version.

However, once you re-compile the application using a new version of GraphicsClass, you will receive a warning from the compiler. For more information, see compiler warning (level 2) CS0108.

This warning indicates that you must consider how the DrawRectangle method works in the application.

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

 
class YourDerivedGraphicsClass : GraphicsClass
{
public override void DrawRectangle() { }
}
 

The override keyword ensures that any object derived from YourDerivedGraphicsClass uses the derived class version of DrawRectangle. Objects derived from YourDerivedGraphicsClass can still use the base keyword to access the base class version of DrawRectangle:

 
base.DrawRectangle();
 

If you do not want your method to override the new base class method, pay attention to the following. To avoid confusion between the two methods, you can rename your method. This may be time-consuming and error-prone, and is not feasible in some cases. However, if your project is relatively small, you can use the reconstruction option of Visual Studio to rename the method. For more information, see refactoring classes and types.

Alternatively, you can use the new keyword in the definition of a derived class to prevent this warning:

 
class YourDerivedGraphicsClass : GraphicsClass
{
public new void DrawRectangle() { }
}
 

The new Keyword tells the compiler that your definition will hide the definition contained in the base class. This is the default action.

Rewrite and Method Selection

When a method is specified in a class, if multiple methods are compatible with the call (for example, there are two methods with the same name, and their parameters are compatible with the passed parameters ), then the C # compiler selects the best method for calling. The following methods will be compatible:

 
public class Derived : Base
{
public override void DoWork(int param) { }
public void DoWork(double param) { }
}
 

When DoWork is called in an instance of Derived, the C # compiler will first try to make the call compatible with the DoWork version originally declared on Derived. The override method is not declared on the class, but a new implementation of the method declared on the base class. Only when the C # compiler cannot match a method call with the original method on Derived can it try to match the call with an override method with the same name and compatible parameters. For example:

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

Since the variable val can be implicitly converted to the double type, the C # compiler calls DoWork (double) instead of DoWork (int ). There are two ways to avoid this situation. First, avoid declaring the new method with the same name as the virtual method. Second, you can forcibly convert the Derived instance to Base to search for the Base class method list by the C # compiler, so that it calls the virtual method. Because it is a virtual method, the implementation of DoWork (int) on Derived will be called. For example:

 
((Base)d).DoWork(val);  // Calls DoWork(int) on Derived.
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.