Information Technology Reference
In-Depth Information
Where Clauses
Constraints are listed as
where
clauses.
Each type parameter that has constraints has its own
where
clause.
If a parameter has multiple constraints, they are listed in the
where
clause, separated by
commas.
The syntax of a
where
clause is the following:
Type parameter
Constraint list
↓
↓
where
TypeParam
:
constraint, constraint, ...
↑
Colon
The important points about
where
clauses are the following:
They are listed after the closing angle bracket of the type parameter list.
They are not separated by commas, or any other token.
They can be listed in any order.
The token
where
is not a keyword, so you can use it in other contexts.
For example, the following generic class has three type parameters.
T1
is unbounded. For
T2
,
only classes of type
Customer
, or classes derived from
Customer
, can be used as type arguments.
For
T3
, only classes that implement interface
IComparable
can be used as type arguments.
Unbounded
With constraints
↓
↓
No separators
class MyClass < T1, T2, T3 >
↓
where T2: Customer // Constraint for T2
where T3: IComparable // Constraint for T3
{
↑
...
No separators
}