Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

25.7 Constraints > 25.7.1 Satisfying constraints - Pg. 713

680 CHAPTER 25: GENERICS implement such introduced interfaces, just as when generics were introduced these types had interfaces such as IComparable<> added to them. The lack of numeric type constraints in C# may limit the use of generics in numerical work. On the other hand, at least if the constraint as opposed to the interface option were selected, this feature would raise further design questions concerning numeric promotion, implicit con- versions, and overloading resolution similar to, but not the same as, issues the other con- straints raised. 25.7.1 Satisfying constraints Whenever a constructed type or generic method is referenced, the supplied type arguments are checked against the type parameter constraints declared on the generic type or method. For each where clause, the type argument A that corresponds to the named type parameter is checked against each constraint as follows: If the constraint is a class type, an interface type, or a type parameter, let C represent that constraint with the supplied type arguments substituted for any type parameters that appear in the constraint. To satisfy the constraint, it shall be the case that type A is conver- tible to type C by one of the following: An identity conversion (§13.1.1) An implicit reference conversion (§13.1.4) A boxing conversion (§13.1.5), provided that type A is a non-nullable value type An implicit reference, boxing, or type parameter conversion from a type parameter A to C (§25.7.4). If the constraint is the reference type constraint, the type A must satisfy one of the following: A is an interface type, class type, delegate type or array type. [Note: Note that System. ValueType and System.Enum are reference types so satisfy this constraint. end note] A is a type parameter that is known to be a reference type (§25.7). If the constraint is the value type constraint, the type A must satisfy one of the following: A is a non-nullable struct type or enum type. [Note: Note that System.ValueType and System.Enum are reference types so do not satisfy this constraint. end note] A is a type parameter having the value type constraint (§25.7). If the constraint is the constructor constraint new(), the type argument A shall not be abstract and shall have a public parameterless constructor. This is satisfied if one of the following is true: A is a value type, since all value types have a public default constructor (§11.1.2). A is a type parameter having the constructor constraint (§25.7). A is a type parameter having the value type constraint (§25.7). A is a class that is not abstract, A contains an explicitly declared public construc- tor with no parameters. A is not abstract and has a default constructor (§17.10.4). A compile-time error occurs if one or more of a type parameter's constraints are not satisfied by the given type arguments. Since type parameters are not inherited, constraints are never inherited either. [Example: In the code below, D needs to specify the constraint on its type parameter T, so that T satisfies the con- straint imposed by the base class B<T>. In contrast, class E need not specify a constraint, because List<T> implements IEnumerable for any T. class B<T> where T: IEnumerable { ...}