Getting Started with Generics

Generics allow developers to define types and methods that are parameterized with type parameters. Those parameters are ultimately filled in when such types or methods are used. Let’s concentrate on generic types for the time being, deferring treatment of methods until later.

When creating a generic type, whether it’s a class, struct, interface, or delegate, you’re adding one or more type parameters to the type declaration. On the other side of the picture, a user of the type can specify types for those parameters. Let’s take a look at an example to set the scene:

class List<T> {    private T[] _elements;    ...    public T this[int i] {        get { return _elements[i]; }        set ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.