1.2. Terminology

In addition to nailing down generic concepts, it's important to establish a clear set of terms that are used to describe the different facets of generics. It's also important for you to have some precision in your generic vocabulary, because many of these terms are referenced heavily throughout the remainder of this book.

First, I'll start by building the shell of a simple generic type that can be referenced as part of this exploration of generic terminology. The following generic Stack type should serve that purpose well:

[VB.NET Example]
Public Class Stack(Of T)
    Private items() as T
    Private count as Integer

    Public Sub Push(item as T)
        ...
    End Sub

    Public Function Pop() as T
        ...
    End Function
End Class

[C# Example]
public class Stack<T> {
    private T[] items;
    private int count;

    public void Push(T item) {...}
    public T Pop() {...}
}

1.2.1. Type Parameters

A type parameter refers to the parameter that is used in the definition of your generic type. In the Stack example, the class accepts one type parameter, T. Each generic type can accept one or more type parameters, and this list of parameters will define the signature of your type. The names used for these parameters are then referenced throughout the implementation of your new type. For the Stack, you can see where multiple references have been added to the Stack's type parameter, T.

Although type parameters can be applied to classes, structs, and interfaces, they cannot be directly applied to indexers, properties, ...

Get Professional .NET 2.0 Generics 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.