Differences between new and override in C #

Source: Internet
Author: User

Differences between new and override in C #
Recently, I briefly reviewed the keywords that need to be used to inherit this feature in C #. Some of the key points are specially sorted out for your convenience. This problem was also mentioned in a Microsoft test and interview:
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 that uses the derived class calls the method defined by the New Keyword, rather than the method of the base class. 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.


2. The Override keyword mainly provides a new implementation of the derived class to the base class method. The overwritten base class method must have the same signature as the Override method, 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.


The following is a small demo that shows the essential differences between new and override:





class Test{static void Main(string[] args){Base ra = new classA();Base rb = new classB();ra.prinf();rb.prinf();}}abstract public class Base{public virtual void fuck(){Console.WriteLine (fuck in Base);}}public class A: Base{public override void fuck(){Console.WriteLine (fuck in class A);}}public class B: Base{public new void fuck(){Console.WriteLine (fuck in class B);}}



Output result:


Fuck in calss

Fuck in class B




3. Virtual key-off 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 ).


Summary:


1. 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. For example, if a subclass is referenced, The subclass is called. If a parent class is referenced, the parent class is called.


2. Override is mainly used to Override the methods of the base class and virtual methods. Both the Child class and parent class reference all point to the Child class object, so they are all members of the Child class.







 

Related Article

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.