Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraint vs Parameter - way to force collection as parameter

I am wondering if there is any difference between these two methods. Second one look more natural, but that shouldn't be the only reason to use it. Maybe there are some performance issues or some diabolic mambojambo related to any of them?

void FirstMethod<T>(T a) where T : IEnumerable<Animal>
{
    ...
}

void SecondMethod<T>(IEnumerable<T> a) where T : Animal
{
    ...
}
like image 947
Bart Juriewicz Avatar asked Nov 17 '14 15:11

Bart Juriewicz


1 Answers

The difference is that you can easily pass IEnumerable<Dog> to the second method, but when you pass it to the first method it'll just be implicitly converted to an IEnumerable<Animal>

Take a look at the fiddle

Edited Thanks @Servy for comment.

like image 180
Max Brodin Avatar answered Oct 30 '22 15:10

Max Brodin