Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantage of using a generics-where-clause call over a non-generic call?

Tags:

c#

generics

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.

like image 961
Pierre-Alain Vigeant Avatar asked Jun 30 '10 18:06

Pierre-Alain Vigeant


People also ask

What are the advantages of using generics?

Generics shift the burden of type safety from you to the compiler. There is no need to write code to test for the correct data type because it is enforced at compile time. The need for type casting and the possibility of run-time errors are reduced. Better performance.

What is the biggest advantage of generics?

One of the big advantages of generics is performance. Using value types with non – generic collection classes results in boxing and unboxing when the value type is converted to a reference type and vice versa.

What is the difference between generic and non generic?

There are only two main differences between generic and brand-name drugs: The inactive ingredients, such as flavoring or preservatives, may change. Generics generally cost less than brand-name versions.

What is generics in Java What are advantages of using generics?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.


2 Answers

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.

like image 155
Jon Skeet Avatar answered Oct 22 '22 15:10

Jon Skeet


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);
like image 1
BFree Avatar answered Oct 22 '22 14:10

BFree