Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

oop

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

like image 917
jaywayco Avatar asked Jul 04 '11 21:07

jaywayco


People also ask

What is the use of override keyword in C#?

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event. An override method provides a new implementation of the method inherited from a base class.

Which keyword is used for overriding?

The override keyword serves two purposes: It shows the reader of the code that "this is a virtual method, that is overriding a virtual method of the base class." The compiler also knows that it's an override, so it can "check" that you are not altering/adding new methods that you think are overrides.

What is difference between new and override in C Plus Plus?

The difference between override and new is that override extend the method of base class with new definition but new hides the method of base class.

What is the difference between override and new keyword?

The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into a derived class. The new keyword is used to hide a method, property, indexer, or event of base class into derived class.


2 Answers

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

like image 77
Nahydrin Avatar answered Sep 24 '22 05:09

Nahydrin


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.

like image 29
Ry- Avatar answered Sep 24 '22 05:09

Ry-