简体   繁体   中英

What is the difference between the override and new keywords in C#?

What is the difference between the override and new keywords in C# when defining methods in class hierarchies?

The following page summarizes your question very nicely.

Knowing When to Use Override and New Keywords

Summary

Override : When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class.

New : If you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it.

If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword).

Override : used with virtual/abstract/override type of method in base class

New : when base class has not declared method as virtual/abstract/override

new will shadow the method with a completely new method (which may or may not have the same signature) instead of overriding it (in which case the new method must have the same signature), meaning that polymorphism won't work. For example, you have these classes:

class A {
    public virtual int Hello() {
        return 1;
    }
}

class B : A {
    new public int Hello(object newParam) {
        return 2;
    }
}

class C : A {
    public override int Hello() {
        return 3;
    }
}

If you do this:

A objectA;
B objectB = new B();
C objectC = new C();

Console.WriteLine(objectB.Hello(null)); // 2
Console.WriteLine(objectC.Hello()); // 3

objectA = objectB;

Console.WriteLine(objectA.Hello()); // 1

objectA = objectC;

Console.WriteLine(objectA.Hello()); // 3

Since you can define new method signatures with new , it's impossible for the compiler to know that the instance of A is actually an instance of B and the new method B defines should be available. new can be used when the parent object's method, property, field or event is not declared with virtual , and because of the lack of virtual the compiler won't “look up” the inherited method. With virtual and override , however, it works.

I would strongly recommend you avoid new ; at best, it's confusing, because you're defining a method with a name that could be recognized as something else, and at worst, it can hide mistakes, introduce seemingly impossible bugs, and make extending functionality difficult.

Looks like an old question, let me try a different answer:

  1. new : as the name says, it is a new member in the family of inheritance hierarchy and this will be used as base member for further down the chain (if marked as virtual).

  2. override : It means I don't accept my parent class' member implementation and I will do differently.

Consider the following class hierarchy:

using System;

namespace ConsoleApp
{     
     public static class Program
     {   
          public static void Main(string[] args)
          {    
               Overrider overrider = new Overrider();
               Base base1 = overrider;
               overrider.Foo();
               base1.Foo();

               Hider hider = new Hider();
               Base base2 = hider;
               hider.Foo();
               base2.Foo();
          }   
     }   

     public class Base
     {
         public virtual void Foo()
         {
             Console.WriteLine("Base      => Foo");
         }
     }

     public class Overrider : Base
     {
         public override void Foo()
         {
             Console.WriteLine("Overrider => Foo");
         }
     }

     public class Hider : Base
     {
         public new void Foo()
         {
             Console.WriteLine("Hider     => Foo");
         }
     }
}    

Output of above codes must be:

Overrider => Foo
Overrider => Foo

Hider     => Foo
Base      => Foo
  • A subclass overrides a virtual method by applying the override modifier :
  • If you want to hide a member deliberately, in which case you can apply the new modifier to the member in the subclass. The new modifier does nothing more than suppress the compiler warning that would otherwise result

override lets you override a virtual method in a base class so that you can put a different implementation in. new will hide a non-virtual method in a base class.

The simple difference is that override means the method is virtual (it goes in conduction with virtual keyword in base class) and new simply means it's not virtual, it's a regular override.

So both really are function overrides, one is with virtual characteristics, the other not.

What does mean exactly? It simply means polymorphism will not be in play for `new' methods.

The following image illustration might make this clear.

在此处输入图像描述

Note if you don't use new keyword, it is still implied but it will generate a warning message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM