A Note on Generics

The second version of the CLI introduced support for generics, a way to make types and members dependent on a certain type of parameter. In the type theory literature this is known as parametric polymorphism. Typical applications of generics include the definition and use of collection types:

var primes = new List<int> { 2, 3, 5, 7 };int firstPrime = primes[0];

In the preceding example we’re using a generic collection type List<T>, where T has been substituted for int. Similarly, we could construct specialized list types to contain strings, Customer objects, or whatever else we see fit (including lists of lists). The use of generics makes static typing possible in many more cases than before, resulting in better compile-time ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.