Reputation: 35277
Given a list of rectangles,
var myList = new List<Rectangle>();
I cannot add anything but Rectangles to this list, so what factors would make me prefer
Rectangle lastRect = myList.Last<Rectangle>();
over simply
Rectangle lastRect = myList.Last();
Upvotes: 1
Views: 227
Reputation: 5812
To support Rubys's answer, the compiler infers the type when you call the Last method without a type parameter.
The final IL that gets generated is exactly the same - I just checked.
Upvotes: 1
Reputation: 3207
These are exactly the same.
The thing is, the compiler uses type inference to understand what the type is, if possible.
It sees you're using a generic function on a generic type, and tries to match between them. And it works.
Short answer: These are the same, but the compiler adds the <Rectangle>
for you, using type inference.
Upvotes: 5