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

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

+48
c # oop
Jul 04 2018-11-21T00:
source share
4 answers

The following page presents your question very well.

Know when to use redefinition and new keywords

Summary

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

New one . If instead of overriding using a new keyword, the method in the derived class does not override the method in the base class, it simply hides it.

If you do not specify either new or overrides, the resulting result will be the same as if you specified a new one, but you will also receive a warning about the compiler (since you may not know that you are hiding the method in the base class method, or you may have wanted to override it, and just forgot to include the keyword).

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

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

+63
Jul 04 2018-11-21T00:
source share

new obscures the method with a completely new method (which may or may not have the same signature) instead of overriding it (in this case, the new method must have the same signature), which means that polymorphism will not work. For example, you have the following 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 , the compiler cannot know that instance A is actually an instance of B , and the new method B must be available. new can be used when the method of the parent object, property, field or event is not declared using virtual , and due to the lack of virtual compiler will not "search" for the inherited method. However, it works with virtual and override .

I highly recommend you avoid new ; at best, this is confusing because you define a method with a name that can be recognized by something else, and in the worst case, it can hide errors, introduce seemingly impossible errors, and make it difficult to extend functionality.

+54
Jul 04 2018-11-11T00:
source share

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

  • new : as the name says, this is a new member in the inheritance hierarchy family, and this will be used as a basic element for further advancement in the chain (if marked as virtual).

  • override : This means that I do not accept the implementation of my parent class, and I will do differently.

+7
Apr 08 '12 at 15:57
source share

override allows you to override the virtual method in the base class so that you can put another implementation. new will hide the non-virtual method in the base class.

+1
Jul 04 2018-11-21T00:
source share



All Articles