In C #, newandoverride

Source: Internet
Author: User

I. in C #, the keyword "new" is frequently used and has three main functions:

A) is used as an operator to create an object and call constructor.

B) as modifier.

C) used to constrain the types of parameters that may be used as type parameters in generic declarations.

In this article, we will only introduce the function of new as a modifier. When used as a modifier, The new Keyword can hide the method of the base class in the derived class, that is to say, the method used in the derived class is to call the method with the New keyword.New definitionInstead of the base class method. If you do not use the New keyword to hide the base class method, the compiler will receive a warning prompting you to use the New keyword to hide the base class method.

Note that the New and Override keywords are mutually exclusive. It cannot be used at the same time.

II. The main keyword of Override isProvides a new implementation of the derived class to the base class method.The Override base class method must be the same as the Override method.Same signatureThis keyword cannot be used to Override non-Virtual and static methods. The keywords used with this keyword are Virtual, abstract, and Override. At the same time, the Override method cannot modify the accessibility of the Virtual method. The Override method and the Virtual method must have the same access modifier, you cannot use modifiers new, static, virtual, or abstract to modify the override method.

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.