
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Creating a Generic Type
|
789
The output of this method is shown here:
Count == 0
Count == 1
Discussion
Type parameters are defined on a class and indicate that any type is allowed to be
substituted for this type parameter (unless of course there are constraints placed on
this type parameter using the
where keyword). For example, the following class has
two type parameters, T and U:
public class Foo<T, U> {...}
Of course you do not have to use T and U; you can instead use another letter or even
a full name such as TypeParam1 and TypeParam2.
A type argument is defined as the actual type that will be substituted for the type
parameter. In the previously defined class
Foo, you can replace type parameter T with
the type argument
int and type parameter U with the type argument string.
The
BindGenericParameters method allows you to substitute type parameters with
actual type arguments. This method accepts a single
Type array parameter. This Type
array consists of each type argument that will be substituted for each type parameter
of the generic type. These type arguments must be added to this
Type array in the
same order as they are defined on the class. For example, the
Foo class defines type
parameters
T and U, in that order. The Type array that you define contains an int type
and a
string type, in that ...