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 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.