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.
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.
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With