The Reference Type and Value Type Constraints
The next two constraints enable you to indicate that a type argument must be either a
reference type or a value type. These are useful in the few cases in which the difference
between reference and value types are important to generic code. Here is the general form
of the reference type constraint:
where
T
: class
In this form of the
where
clause, the keyword
class
specifies that
T
must be a reference type.
Thus, an attempt to use a value type, such as
int
or
bool
, for
T
will result in a compilation
error.
Here is the general form of the value type constraint:
where
T
: struct
In this case, the keyword
struct
specifies that
T
must be a value type. (Recall that structures
are value types.) Thus, an attempt to use a reference type, such as
string
, for
T
will result in
a compilation error. In both cases, when additional constraints are present,
Do'stlaringiz bilan baham: |