Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

4.4 Constructed Types

A generic type declaration, by itself, denotes an unbound generic type that is used as a “blueprint” to form many different types, by way of applying type arguments. The type arguments are written within angle brackets (< and >) immediately following the name of the generic type. A type that includes at least one type argument is called a constructed type. A constructed type can be used in most places in the language in which a type name can appear. An unbound generic type can be used only within a typeof-expression (§7.6.11).

Constructed types can also be used in expressions as simple names (§7.6.2) or when accessing a member (§7.6.4).

When a namespace-or-type-name is evaluated, only generic types with the correct number of type parameters are considered. Thus it is possible to use the same identifier to identify different types, as long as the types have different numbers of type parameters. This is useful when mixing generic and nongeneric classes in the same program:

namespace Widgets
{
    class Queue {...}
    class Queue<TElement> {...}
}

namespace MyApplication
{
    using Widgets;

    class X
    {
        Queue q1;               // Nongeneric Widgets.Queue
        Queue<int> q2;          // Generic Widgets.Queue
    }
}

A type-name might identify a constructed type even though it doesn’t specify type parameters directly. This can occur where a type is nested within a generic class declaration, and the instance type of the containing declaration is implicitly used for name lookup (§10.3.8.6):

class Outer<T>
{
    public class Inner {...}

    public Inner i;                     // Type of i is Outer<T>.Inner
}

In unsafe code, a constructed type cannot be used as an unmanaged-type (§18.2).

4.4.1 Type Arguments

Each argument in a type argument list is simply a type.

type-argument-list:
    < type-arguments >

type-arguments:
    type-argument
    type-arguments , type-argument

type-argument:
    type

In unsafe code (§18), a type-argument may not be a pointer type. Each type argument must satisfy any constraints on the corresponding type parameter (§10.1.5).

4.4.2 Open and Closed Types

All types can be classified as either open types or closed types. An open type is a type that involves type parameters. More specifically:

  • A type parameter defines an open type.
  • An array type is an open type if and only if its element type is an open type.
  • A constructed type is an open type if and only if one or more of its type arguments is an open type. A constructed nested type is an open type if and only if one or more of its type arguments or the type arguments of its containing type(s) is an open type.

A closed type is a type that is not an open type.

At runtime, all of the code within a generic type declaration is executed in the context of a closed constructed type that was created by applying type arguments to the generic declaration. Each type parameter within the generic type is bound to a particular runtime type. The runtime processing of all statements and expressions always occurs with closed types, and open types occur only during compile-time processing.

Each closed constructed type has its own set of static variables, which are not shared with any other closed constructed types. Since an open type does not exist at runtime, there are no static variables associated with an open type. Two closed constructed types are the same type if they are constructed from the same unbound generic type, and their corresponding type arguments are the same type.

4.4.3 Bound and Unbound Types

The term unbound type refers to a nongeneric type or an unbound generic type. The term bound type refers to a nongeneric type or a constructed type.

An unbound type refers to the entity declared by a type declaration. An unbound generic type is not itself a type, and it cannot be used as the type of a variable, argument, or return value, or as a base type. The only construct in which an unbound generic type can be referenced is the typeof expression (§7.6.11).

4.4.4 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 (§10.1.5). 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 must be the case that type A is convertible to type C by one of the following:
    • An identity conversion (§6.1.1).
    • An implicit reference conversion (§6.1.6).
    • A boxing conversion (§6.1.7), 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.
  • If the constraint is the reference type constraint (class), the type A must satisfy one of the following:
    • A is an interface type, class type, delegate type, or array type. Both System.ValueType and System.Enum are reference types that satisfy this constraint.
    • A is a type parameter that is known to be a reference type (§10.1.5).
  • If the constraint is the value type constraint (struct), the type A must satisfy one of the following:
    • A is a struct type or enum type, but not a nullable type. Both System.ValueType and System.Enum are reference types that do not satisfy this constraint.
    • A is a type parameter having the value type constraint (§10.1.5).
  • If the constraint is the constructor constraint new(), the type A must not be abstract and must 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 (§4.1.2).
    • A is a type parameter having the constructor constraint (§10.1.5).
    • A is a type parameter having the value type constraint (§10.1.5).
    • A is a class that is not abstract and contains an explicitly declared public constructor with no parameters.
    • A is not abstract and has a default constructor (§10.11.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. In the example below, D needs to specify the constraint on its type parameter T so that T satisfies the constraint 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 {...}

class D<T>: B<T> where T: IEnumerable {...}

class E<T>: B<List<T>> {...}
  • + Share This
  • 🔖 Save To Your Account