Negative tive C # item29: The new modifier is considered only when the new base class is faulty.

Source: Internet
Author: User

We generally use the new modifier on Class Members to redefine non-Virtual members inherited from the base class. However, we do not recommend that you do this, this is because redefinition of non-virtual methods can lead to ambiguous behaviors.

Non-virtual methods are statically bound, and the compiler does not determine which method should be called Based on the object runtime type. virtual functions use dynamic binding, the compiler determines the method to be called Based on the runtime type of the object.

Avoid using the new modifier to redefine non-virtual methods. It does not mean that you should design all methods in the base class as virtual methods. When the library designer defines a method as a virtual method, it actually creates a contract for the type, indicating that any derived class can change the implementation of the virtual method. In fact, the virtual method set defines all the behavior that may change in the derived class. The design of "default setting as virtual method" indicates that the derived class can change all behaviors of the parent class, which means that we did not carefully consider what behavior the derived class will change, this is inappropriate. We should spend more time carefully considering which methods and attributes should be declared as polymorphism members. We should declare these members as virtual members only. Do not consider this practice as a restriction on class users. Instead, we should regard this practice as an entry point for custom type behavior.

Only in one case, we need to use the new modifier to redefine the virtual method: After we use the new base class, it is found that the newly added method name in the base class conflicts with the existing method name in the sub-column.

 

Conclusion: when using the new modifier, we should be very careful. If we use it indiscriminately, there will be ambiguous method calls on the object. The new modifier is considered only when the new base class addition method conflicts with the existing method in the subclass. Even in this case, we should consider it with caution. In addition, we should not use the new modifier in any other situation.

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.