C# Frequently Asked Questions

The C# team posts answers to common questions

What's the difference between override and new?

This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of the object that the reference refers to is used to decide which method implementation to use. 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. For instance:

public class Base
{
    public virtual void SomeMethod()
    {
    }
}

public class Derived : Base
{
    public override void SomeMethod()
    {
    }
}

...

Base b = new Derived();
b.SomeMethod();

will end up calling Derived.SomeMethod if that overrides Base.SomeMethod. Now, 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. In that case, code like this:

public class Base
{
    public virtual void SomeOtherMethod()
    {
    }
}

public class Derived : Base
{
    public new void SomeOtherMethod()
    {
    }
}

...

Base b = new Derived();
Derived d = new Derived();
b.SomeOtherMethod();
d.SomeOtherMethod();

Will first call Base.SomeOtherMethod (line 3), then Derived.SomeOtherMethod (line 4). They're effectively two entirely separate methods which happen to have the same name, rather than the derived method overriding the base method.

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).

That provides the basics of overriding and the difference between new and override, but you should really see a book or tutorial for a more in-depth look at polymorphism.

[Author: Jon Skeet]

Published Friday, March 12, 2004 1:30 AM by CSharpFAQ

Comments

 

Russ said:

Another difference:
virtual - good practice
new - well, let's just say you'd better have a really good reason for using it!

:-)
March 12, 2004 6:00 AM
 

Fabrice said:

You forgot the virtual keyword in your first example.
March 12, 2004 6:07 AM
 

Jonathan Pryor said:

The Base.SomeMethod method needs to be declared virtual, or you'll get a compiler warning/error about trying to override a non-virtual function.
March 12, 2004 6:08 AM
 

Jon Skeet said:

Doh! Thanks...
March 13, 2004 12:17 AM
 

Rick Byers said:

You might also want to mention what the motivation was for C# to have 'new' and 'override' keywords when no other pre-.NET language (that I'm aware of) does.

Namely, to solve a form of the fragile base class problem. A new version of a base class may be released which happens to introduce a method with the same name as a method in your derived class. Since these two methods have no relationship to eachother, except that the happen to have the same name, you certainly don't want to start overriding the new base method.
March 17, 2004 11:06 AM
 

Mike B said:

This'll help me for my midterm =). Thanks!
March 23, 2004 8:47 AM
 

Bertrand said:

It should also be mentioned that one of the few legitimate uses of new is to change the return type of a method or property. It can help making a derived class more strongly typed than the base. We use it for that in ASP.NET in a few places (i.e. adapters)
April 14, 2004 11:31 AM
 

Ramprakash said:


Another key difference is when you do an up cast, ancestor version of function will be executed instead of the descendant, if you have overriden function it will execute the overriden version of your function instead of the ancestor version.

April 27, 2004 3:03 PM
 

amit said:

aaa
July 21, 2004 10:27 PM
 

RebelGeekz said:

December 28, 2004 4:54 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker