Type parameter constraints (C # programming guide)
Updated: February 1, July 2008
When defining generic classes, you can impose restrictions on the types of types that client code can use for type parameters when instantiating classes. If the client code attempts to instantiate a class using a type not allowed by a certain constraint, a compile-time error will occur. These restrictions are called constraints. The constraint is specified using the where context keyword. The following table lists the six types of constraints:
|
Constraints |
Description |
|
T: Structure |
The type parameter must be a value type. You can specify any value type except Nullable. For more information, see use a void type (C # programming guide ). |
|
T: Class |
The type parameter must be of the reference type. This applies to any class, interface, delegate, or array type. |
|
T: new () |
The type parameter must have a public constructor without parameters. When used with other constraints, the new () constraint must be specified at the end. |
|
T: <Base Class Name> |
The type parameter must be a specified base class or derived from the specified base class. |
|
T: <Interface Name> |
The type parameter must be a specified interface or implement the specified interface. Multiple interface constraints can be specified. The constraint interface can also be generic. |
|
T: U |
The type parameter provided for T must be provided for U or derived from U. This is called the bare type constraint. |
Use constraints to constrain multiple parameters
You can apply constraints to multiple parameters and apply multiple constraints to one parameter, as shown in the following example:
C #
class Base { }class Test<T, U>where U : structwhere T : Base, new() { }
Unbound type parameter
A type parameter without constraints (for example, T in the public class SampleClass <T >{}) is called an unbound type parameter. Unbound type parameters have the following rules:
-
Not available! = And = operators, because they cannot be supported by specific type parameters.
-
You canSystem. ObjectOr explicitly convert them to any interface type.
-
You can compare them with null. When you compare unbound parameters with null, if the type parameter is of the value type, the comparison always returns false.
Bare type constraints
The generic type parameters used as constraints are called Bare type constraints. When a member function with its own type parameter must constrain this parameter to a type parameter containing the type, the bare type constraint is useful, as shown in the following example:
C #
class List<T>{void Add<U>(List<U> items) where U : T {/*...*/}}
In the preceding example, TAddThe method context is a bare type constraint, whileListClass is an unbound type parameter in the context.
Bare type constraints can also be used in generic class definitions. Note that the bare type constraint must be declared in angle brackets with any other type parameter:
C #
//naked type constraintpublic class SampleClass<T, U, V> where T : V { }
The bare type constraints of generic classes have very limited functions, because the compiler assumes that a bare type constraint is derived fromSystem. ObjectNo other assumptions. When you want to force the inheritance relationship between two type parameters, you can use the bare type constraint for the generic class.