Previous Page
Next Page

Chapter 17 Quick Reference

To

Do this

Example

Instantiate an object by using a generic type.

Specify the appropriate type parameter.

Queue<int> myQueue =
    new Queue<int>();

Create a new generic type.

Define the class by using a type parameter.

public class Tree<T>
{
    ...
}

Restrict the type that can be substituted for the type parameter.

Specify a constraint by using a where clause when defining the class.

public class Tree<T>
where T : IComparable<T>
{
    ...
}

Define a generic method.

Define the method by using type parameters.

static Tree<T> BuildTree<T>
(params T[] data)
{
    ...
}

Invoke a generic method.

Provide types for each of the type parameters.

Tree<char> charTree =
  BuildTree<char>('Z', 'X');

Previous Page
Next Page