tags:

views:

81

answers:

2

I've seen some examples where they transformed a call like

void Add(IDrawing item);

into

void Add<TDrawing>(TDrawing item) where TDrawing : IDrawing;

Beside tricking the intellisense into displaying the name of your class instead of the interface name when calling the function, because of inferred type usage in C#4, are there any other advantage to using the second approach?

To answer Jon Skeet, the code our programmer used is:

public ObservableCollection<IDrawing> Items { get; private set; }

public void Add<TDrawing>(TDrawing item) where TDrawing : IDrawing
{
   this.Items.Add(item);
}

I don't see any advantage here for using a generic instead of just using a parameter of the IDrawing type. I presume there must be some case where its very appropriate. I was curious to see if I was missing something.

+4  A: 

It really depends on what's going on elsewhere in the implementation. Here's a different example:

void Add<TDrawing>(TDrawing item, IList<TDrawing> list)
    where TDrawing : IDrawing
{
    if (item.SomePropertyOfIDrawing)
    {
        list.Add(item);
    }
}

Now you wouldn't want to take an IList<IDrawing> here - because then you couldn't use it if you had a List<Painting> for example... whereas with the generic version above, it's absolutely fine for TDrawing to be Painting: the constraint ensures that property is available for the if condition, and the fact that it's generic allows you to safely add the item to the list.

If you have full examples where you don't think there's a benefit, it would be worth presenting those specifically.

EDIT: No, in the exact example now given there's no advantage in making it a generic method.

Jon Skeet
Indeed. Much more useful at the level of a generic class, so that the whole class has type knowledge based on the generic constraints.
Cylon Cat
That's adding something to the original example though (the need to keep both parameters of the same type)...the analog to the original example wouldn't be just changing to list to IList<IDrawing> but also TDrawing item to IDrawing item. I mean, its interesting and all but doesn't really answer what he's asking.
kekekela
@kekekela: Originally there wasn't nearly enough context - now there's a fuller example, it's easier to answer.
Jon Skeet
Maybe the programmer was in a generic mood and was writing everything in a generic way. Though, I like the fact that the intellisense now display the class name instead of the interface name. Upon digging a little in some possible alternative, I think that it could be useful when creating a function that require two interface in an object: like `void Add<TDrawing>(TDrawing item) where TDrawing : IDrawingItem, ISomethingElse`
Pierre-Alain Vigeant
+1  A: 

Think of this scenario:

void Add<TDrawing>(TDrawing item, Func<TDrawing, bool> func)
{
   //implementation
}

Now when calling this method, at compile time, you'll be able to access the specific proeprties of the specific TDrawing being passed into this method to use with the Func.

Add<MyDrawing>(drawing, m => m.SomeMyDrawingProp);
BFree