Information Technology Reference
In-Depth Information
Constraint Types and Order
There are five types of constraints. These are listed in Table 19-2.
Table 19-2. Types of Constraints
Constraint Type Description
ClassName Only classes of this type, or classes derived from it, can be used as the type
argument.
class Any reference type, including classes, arrays, delegates, and interfaces, can be
used as the type argument.
struct Any value type can be used as the type argument.
InterfaceName Only this interface, or types that implement this interface, can be used as the
type argument.
new()
Any type with a parameterless public constructor can be used as the type
argument. This is called the constructor constraint .
The where clauses can be listed in any order. The constraints in a where clause, however,
must be placed in a particular order, as shown in Figure 19-9.
￿
There can be at most one primary constraint, and if there is one, it must be listed first.
There can be any number of InterfaceName constraints.
￿
￿
If the constructor constraint is present, it must be listed last.
Figure 19-9. If a type parameter has multiple constraints, they must be in this order.
The following declarations show examples of where clauses:
class SortedList<S>
where S: IComparable<S> { ... }
class LinkedList<M,N>
where M : IComparable<M>
where N : ICloneable { ... }
class MyDictionary<KeyType, ValueType>
where KeyType : IEnumerable,
new() { ... }
Search WWH ::




Custom Search