Differences between overload override virtual new

Source: Internet
Author: User

overload: methods with the same name in the same class. They have different numbers of parameters or different parameter types. It is used for the implementation of methods with the same name but different numbers of parameters or types in the same class, that is, to make the methods have different signature versions. Different parameters are used for distinguishing.

  Public   string  S () 
{< br> return " ";< BR >}< br> Public int S () // error reported. A method named" S "with the same method signature has been defined.
{< br> return 1 ;< BR >}< br> Public int S ( string T) // correct, different method signatures
{
return 1 ;
}

Override: Method overwrite. Defining a method in a subclass has the same name and parameters as its parent class. When the subclass object uses this method, the definition in the subclass is called. For this method, the definition in the parent class is "blocked. This 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. If the method is virtual and the subclass uses override, the compiler produces additionalCode. Then, during running, check the class of the object and call the method of the class.

 

Virtual: the virtual keyword allows you to override these objects in a derived class. By default, the method is non-virtual and cannot be rewritten, virtual keywords cannot be used with static, abstract, private, or override. The virtual keyword is closely related to override. To implement the virtual method, you must use the override or New Keyword (new and override generate different mechanisms ).

Conclusion: The New Keyword is mainly used to distinguish between a derived class and a method with the same name as the base class. By hiding the base class method, the compiler can call the correct method. Override is mainly used
And virtual methods.

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.