tags:

views:

86

answers:

2

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();
+5  A: 

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.

Rubys
+1  A: 

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.

David_001