Deep understanding of the difference between new, override and virtual keywords in C # _c# Tutorials

Source: Internet
Author: User
Tags modifier modifiers

Oo thinking is now widely used in software development projects, one of the most important feature is the inheritance, recently I briefly reviewed in C # to inherit this feature, the need to use the keyword, which has some key points, specially sorted out, convenient for everyone to consult.

First, in C #, new this keyword uses very high frequency, has 3 main functions:

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

b) as modifiers.

c) is used to constrain the types of parameters that might be used as type parameters in a generic declaration.

In this article, only the role of new as a modifier is specifically described, when used as a modifier, the new keyword hides the method of the base class in a derived class, which means that the method used to invoke a derived class is a method that is newly defined by the new keyword, rather than a method of the base class. It is also possible to hide the base class method without using the New keyword, and the compiler will appear with a warning that if you deliberately want to hide the method of the base class, use the new keyword to modify it.

Here's a note that the two keywords new and override are mutually exclusive. cannot be used at the same time.

second, the override keyword is mainly to provide derived classes of the new implementation of the base class method , the overridden base class method must have the same signature as the override method, this keyword cannot be used to override Non-virtual methods and static methods, and its matching keyword is virtual, Abstract, Override. At the same time, the override method cannot modify the accessibility of the virtual method, override methods and virtual methods must have the same access modifiers and cannot be modified using modifiers new, static, virtual, or abstract Override method.

Here's a little demo that shows the essential difference between new and override:

Class program

  {

    static void Main (string[] args)

    {     

      Class1 ct1 = new Class1 ();

      Contact CT2 = new Class2 ();

      Ct1.prinf ();

      Ct2.prinf ();

    }

  Abstract public class

  contacts {public

    virtual void Prinf ()

    {

      Console.WriteLine ("This is a virtual method");

    }

  }

  public class Class1:contact

  {public

    override void Prinf ()

    {

      Console.WriteLine ("This is the New method");

    }

  }

  public class Class2:contact

  {public

    new void Prinf ()

    {

      Console.WriteLine ("This is another new method");

    }

}

The results of this demo run are:

This is the new way.

It's a virtual method.

The virtual keyword allows you to override these objects in a derived class by default, the method is non-virtual and the Non-virtual method cannot be overridden , and the virtual keyword cannot be associated with static, abstract, private, Override used together. Virtual keyword is closely related to override, if you want to implement the virtual method must use override or New keyword (new and override produced different mechanism).

Summary: The new keyword is used primarily to distinguish between derived and base class methods with the same name, and to make the compiler invoke the correct method by hiding the base class method. Override is primarily used to override the methods and virtual methods of the base class.

The above in-depth understanding of C # in new, override, virtual keyword is the difference is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.