Effective C # Principle 29: Use the new modifier only when a forced update is made to the base class

Source: Internet
Author: User
Tags inheritance modifier

You can redefine a non-virtual member inherited from a base class with the new modifier. You can do this, but that doesn't mean you need to do that. Redefining non-virtual methods can cause confusion in the meaning of the method. If two related classes are inheritance relationships, many developers may immediately assume that two blocks of code are doing exactly the same thing, and they think so:

object c = MakeObject( );
// Call through MyClass reference:
MyClass cl = c as MyClass;
cl.MagicMethod( );
// Call through MyOtherClass reference:
MyOtherClass cl2 = c as MyOtherClass;
cl2.MagicMethod( );

Once the new modifier is used, the problem is completely different:

public class MyClass
{
 public void MagicMethod( )
 {
  // details elided.
 }
}
public class MyOtherClass : MyClass
{
 // Redefine MagicMethod for this class.
 public new void MagicMethod( )
 {
  // details elided
 }
}

This practice can be confusing to many developers. Because when you invoke the same function on the same object, you must expect them to execute the same code. But actually, when you invoke a function of the same name with a different reference, they behave differently, which is a very bad feeling. They are inconsistent. An object of a Myotherclass type behaves differently depending on how you refer to it. This is the consequence of the new modifier used on non-virtual members. In fact, it just lets you add a different method to the namespace of the class (although their function names are the same).

Non-virtual methods are statically bound, no matter where the code is or where it is referenced, Myclass.magicmethod () is always strictly calling the function defined in the class. Does not look for a different version in a derived class at run time. On the other hand, virtual functions are dynamic. The runtime invokes different versions based on different types of objects.

It is recommended that you avoid using the new modifier to redefine a non-virtual function, which is not too much of an explanation, as it is recommended that you use a virtual method when defining a base class. The designer of a class library should design virtual functions according to a certain type. It means that you expect any derived class to modify the implementation of the virtual function. A set of virtual functions is equivalent to defining a set of behaviors that are expected to be implemented again in the derivation. Designing a default virtual function means that the derivation can modify all the virtual behaviors in the class. This does mean that you don't want to consider the differences that all derived classes might want to modify behavior. Instead, you can spend time thinking about what methods and attributes are designed to be polymorphic. Of course, it is only when they are imaginary that they do so. Do not consider restricting the users of the class. Instead, it should be considered to provide a portal wizard for the user-defined behavior of the type.

There is and only one case to use the new modifier, which is when the class is integrated into an existing base class, and this base class already uses the existing method name, and then it's time to use new (meaning that both the base class and the derived class already exist, the inherited relationship that was added later, and the result is that when you add an inheritance relationship, Finding that the same method name is used in two classes, you can add a new to the derived class to solve the problem. Because some code is already lazy on the class's method name, or there are other assemblies that are using this method. For example, you created the following class in the library, using the Basewidget defined in another library:

public class MyWidget : BaseWidget
{
 public void DoWidgetThings( )
 {
  // details elided.
 }
}

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.