16

Possible Duplicate:
C# keyword usage virtual+override vs. new
Difference between new and override?

So I've been working on a project and decided to do some reading about the difference between the new and override keywords in C#.

From what I saw, it seems that using the new keyword functionality is a great way to create bugs in the code. Apart from that I don't really see when it would actually make sense to use it.

More out of curiosity than anything, is there any pattern where the new keyword is the right way to go?

Community
  • 1
  • 1
user472875
  • 3,115
  • 4
  • 37
  • 68
  • http://msdn.microsoft.com/en-us/library/ms173153(v=vs.80).aspx – myermian Mar 06 '12 at 19:13
  • 2
    You may notice I wasn't asking what the difference between the two keywords is. The question was whether or not there is a common pattern where you would intentionally want to hide base class members in this way. – user472875 Mar 06 '12 at 19:25
  • 3
    @Ramhound - It makes complete sense if you stop thinking so literally. Mis-use of the new keyword when inheriting from a parent class can lead to many bugs when used improperly (developers often throw it on a method they mean to override but isn't virtual...which leads to very unexpected behavior). – Justin Niessner Mar 06 '12 at 19:27
  • There is an actual use... when you want to be more specific... for example, if the base class returns an object, but you want your derived class to return a string using the same method signature. – myermian Mar 06 '12 at 19:30
  • 2
    Well done moderators. The question was 'when to use new' and you close it as a duplicate of completely different questions. Sadly, none of the answers really managed to answer the question because you just had to close it within 13 minutes from creating. You must feel so proud. – Bartosz Jul 10 '19 at 08:37

3 Answers3

9

The new keyword is the right way to go when you want to hide a base implementation and you don't care that you won't be able to call your method when treating your object polymorphically.

Let me clarify. Consider the following:

public class Base
{
    public void DoSomething()
    {
        Console.WriteLine("Base");
    }
}

public class Child : Base
{
    public new void DoSomething()
    {
        Console.WriteLine("Child");
    }
}

From time to time, it is beneficial to hide the base class' functionality in the child hence the use of the new keyword. The problem is that some developers do this blindly which will lead to the following side effect (hence the 'bugs' you mention):

Base instance = new Child();
instance.DoSomething(); // may expect "Child" but writes "Base"
Matthew Andrews
  • 437
  • 1
  • 5
  • 16
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • this is not true (at least how I am reading it). If the base method is marked virtual, you can still call it from the 'new' method. Did you mean something else? –  Mar 06 '12 at 19:19
  • 1
    @Joe - Do you know the difference between a `public constructor` and a `public method`? It seems your not talking about new SomeObject( value ) but instead of something else. You really should clarify what your talking about. – Security Hound Mar 06 '12 at 19:21
  • 2
    @Joe - I did mean something different. Consider you have a base class called Base with a method called DoSomething. You inherit Base in Child and create a DoSomething method using the new keyword. If you do the following: `Base base = new Child(); base.DoSomething();` you will be calling `Base.DoSomething` rather than `Child.DoSomething` because you hid the base implementation in Child rather than overriding it. – Justin Niessner Mar 06 '12 at 19:21
  • OK, I read that wrong. Thanks for the clarification –  Mar 06 '12 at 19:26
  • 1
    @Joe - Yes. It is valid. But the two statements do something completely different with completely different behaviors from each other. – Justin Niessner Mar 06 '12 at 19:29
  • Can you edit the answer so I can remove the DV? :) –  Mar 06 '12 at 19:31
  • "The problem is that some developers do this blindly which will lead to the following side effect (hence the 'bugs' you mention)", biggest issue with new keyword in day to day workings. – Sully Feb 26 '14 at 14:32
1

One thing new gives you is the ability to make it appear as if you can override a method that is not declared virtual. Keep in mind you are not overriding, but in fact declaring a method with the same name.

To use override, the method has to be marked as virtual or abstract.

public class PolyTest
{
    public void SomeMethod()
    {
        Console.WriteLine("Hello from base");
    }
}

public class PolyTestChild : PolyTest
{
    new public void SomeMethod()
    {
        Console.WriteLine("Hello from child");
    }
}

Regarding your comment about buggy code, If the base implementation is doing something that you or the caller is expecting, that is where the bug is. However, I do not see that as any more dangerous than failing to call base.SomeMethod (when overriding) and you need to.

  • 2
    Be careful with your wording. new doesn't ever allow you to override anything. It allows you to hide the base implementation. – Justin Niessner Mar 06 '12 at 19:19
  • 1
    That's definitely not an override; it's a standard "create a new method on the derived class that just happens to have the same name as a method on the base class." If a method isn't marked `virtual` or `abstract`, then you can't override it. – dlev Mar 06 '12 at 19:23
  • in the purest sense of the word `override` you are correct, but the OP was asking what the difference is. I will edit to the correct terminology –  Mar 06 '12 at 19:29
0

new has its place and it is only considered a great way to right buggy code if you don't know how to use it.

One example:

public class A
{
    public void AMethod() { }
}

public class B : A
{
    public override void AMethod() { }
}

In this situation AMethod cannot be overriden by B because it is not virtual or abstract, so in order to provide a custom implementation you must use new.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • Using neither override nor new would give the same result as using new except that you'd get a warning at compile time. – rlesias Jul 05 '17 at 16:46