In depth understanding of the differences between new override and virtual keywords in C

  • 2021-10-16 02:28:15
  • OfStack

OO thought has now been widely used in software development projects, one of the most important features is inheritance. Recently, I simply reviewed the keywords needed when it comes to inheritance in C #, among which there are 1 key points, which are specially sorted out for everyone's reference.

1. In C #, the keyword new is used very frequently and has three main functions:

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

b) as a modifier.

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

In this article, only the role of new as a modifier is specifically introduced. When used as a modifier, new keyword can hide the method of the base class in the derived class, that is to say, when using the method of the derived class, the method called is a newly defined method of New keyword, not a method of the base class. It is also possible to hide base class methods without using the New keyword. A warning will appear in the compiler, suggesting that if you intend to hide base class methods, please use New keyword modification.

One note here is that the two keywords New and Override are mutually exclusive. Can't be used at the same time.

2. Override keyword mainly provides a new implementation of derived classes to base class methods. Overridden base class methods must have the same signature as Override methods. This keyword cannot be used to override non-virtual methods and static methods. The keywords used with it 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, and the modifiers new, static, virtual or abstract cannot be used to modify the override method.

The following is a small demo, which 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 contact

  {

    public virtual void prinf()

    {

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

    }

  }

  public class class1 : contact

  {

    public override void prinf()

    {

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

    }

  }

  public class class2 : contact

  {

    public new void prinf()

    {

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

    }

}

This demo runs as follows:

This is a new method

This is a virtual method

3. The Virtual keyword allows these objects to be overridden in derived classes. By default, methods are non-virtual, and non-virtual methods cannot be overridden. The virtual keyword cannot be used with static, abstract, private, override1. Virtual keyword is closely related to override. If you want to implement Virtual method, you must use override or new keyword (new and override have different generation mechanisms).

Summary: New keyword is mainly used to distinguish the selection of methods with the same name between derived classes and base classes. By hiding base class methods, the compiler can call the correct methods. Override is primarily used to override base class methods and virtual methods.


Related articles: