5

The examples that I have come across for when "new" makes sense involve maintenance situations where the sub class is inheriting from a base class in a library, and a new version of the library adds a method with the same name as one that has been implemented in the sub class. (See: Brittle Base Classes)

What I am wondering is if there are instances where the use of "new" is a good design choice (rather than something that can become necessary in maintenance). One thing that can't be done with override is changing the return type of a method/property while keeping the name the same, but I question whether it's ever a good design choice.

public class FruitBasket {
    public int Weight { get; set;}
    public List<Fruit> Fruits {get; set;}
}

public class AppleBasket : FruitBasket {
    public new List<Apple> Fruits  {get; set;}
}
lgaud
  • 2,430
  • 20
  • 30
  • 1
    Doesn't answer your question - but an interesting post on one use (see answer from Mr. Lippert): http://stackoverflow.com/questions/4348760/c-sharp-covariant-return-types-utilizing-generics – dugas Mar 14 '12 at 19:51
  • 1
    [This](http://stackoverflow.com/a/3838692/1583) SO answer to another question details two scenarios where method hiding can be a good design. – Oded Mar 14 '12 at 20:18
  • The sample from Eric Lippert certainly seems a lot cleaner than the code I posted, which leads to all kinds of fun things when you store an AppleBasket in a FruitBasket variable. – lgaud Mar 14 '12 at 20:25

2 Answers2

0

One situation I know of is that you have to use new in .Net WinForms if you are databinding to an interface that inherits from another interface, and you want to bind to members in the base interface.

For example if you have

public interface IOne
{
   int ID {get;set;}
   string Code {get;set;}
}

public interface ITwo : IOne
{
   DateTime CreatedDate {get;set;}
}

and you databind a control to a ITwo object, then you won't be able to see the ID or Code properties unless you add them to the ITwo interface. Of course you don't have to use new, but it is recommended.

Other than that, I have only ever used it in maintenance mode, i.e. once an app has shipped and new requirements necessitate such a change.

HTH, Dean.

DeanOC
  • 7,142
  • 6
  • 42
  • 56
  • Would this apply to derived classes and/or WPF binding as well? Or is it very specific to interfaces in WinForms? – lgaud Mar 19 '12 at 11:23
  • I don't know about WPF binding, but the issue doesn't arise if you bind to concrete classes instead of interfaces. However if you bind to concrete classes then you lose flexibility when comes to testing with mocks. – DeanOC Mar 19 '12 at 19:17
-1

I think the MSDN reference shows where the new keyword can be justified: (MSDN reference: new Modifier). For instance, cases of using nested classes.

code4life
  • 15,655
  • 7
  • 50
  • 82
  • 1
    The MSDN documentation doesn't comment on whether they think their scenarios are good design decisions, just that it can be done :) – lgaud Mar 14 '12 at 20:12
  • Agreed. But the use of nested classes in the example provides an interesting guidance on *how* it can be used, and this in turn can help you understand that more than just methods are affected in the design decision. – code4life Mar 14 '12 at 20:18