Use the override and new keywords to control the version

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 inherited method or a new method that only hides an inherited method with a similar name.

C # Allow the derived class to contain methods with the same name as the 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 without calling 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 not a virtual method. 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.

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.