O'Reilly logo

Professional .NET 2.0 Generics by Tod Golding

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required