If I have a class like this: -
static class Foo {
public static void Bar<T>(T item) { Console.WriteLine(item.ToString(); }
}
I know that in this example it's unnecessary to use T since all Types have ToString() on them etc. - it's simply a contrived example. What I'm more interested in is what happens under the bonnet in terms of the following: -
Foo.Bar("Hello");
Foo.Bar(123);
Foo.Bar(new Employee("Isaac"));
I broadly (think!) I understand reification i.e. if you make different types of a generic class e.g.
List<Int32>
List<String>
List<Employee>
etc. then at compile-time (or runtime?) we end up with three actual concrete types, one for each generic argument specified. Does the same apply to method calls in my first example i.e. would we still have a single class Foo but three reified Bar methods, one for String, Int32 and Employee?
T
. But there is one good reason: You don't get boxing ifT
is a value type. That's nice. So there is a reason to use aT
type parameter (instead of justobject
) even when there are nowhere T : something
constraints. – Getupandgo