System.Type.IsGenericType Property

Gets a value indicating whether the current type is a generic type.

Syntax

public virtual bool IsGenericType { get; }

Value

true if the current type has type arguments; otherwise false.

Remarks

Use the Type.IsGenericType property to determine whether a Type object represents a generic type. Use the Type.ContainsGenericParameters property to determine whether a Type object represents an open constructed type or a closed constructed type.

Note:

The Type.IsGenericType property returns false if the immediate type is not generic. For example, an array whose elements are of type A<int> (A(Of Integer) in Visual Basic) is not itself a generic type.

The following table summarizes the invariant conditions for common terms used in generic reflection.

generic type definition

The Type.IsGenericTypeDefinition property is true.

Defines a generic type. A constructed type is created by calling the Type.MakeGenericType(Type[]) method on a Type object that represents a generic type definition and specifying an array of type arguments.

Type.MakeGenericType(Type[]) can be called only on generic type definitions.

Any generic type definition is a generic type (the Type.IsGenericType property is true), but the converse is not true.

generic type

The Type.IsGenericType property is true.

Can be a generic type definition, an open constructed type, or a closed constructed type.

Note that an array type whose element type is generic is not itself a generic type. The same is true of a Type object representing a pointer to a generic type.

open constructed type

The Type.ContainsGenericParameters property is true.

Examples are a generic type that has unassigned type parameters, a type that is nested in a generic type definition or in an open constructed type, or a generic type that has a type argument for which the Type.ContainsGenericParameters property is true.

It is not possible to create an instance of an open constructed type.

Note that not all open constructed types are generic. For example, an array whose element type is a generic type definition is not generic, and a pointer to an open constructed type is not generic.

closed constructed type

The Type.ContainsGenericParameters property is false.

When examined recursively, the type has no unassigned generic parameters.

generic type parameter

The Type.IsGenericParameter property is true.

The Type.ContainsGenericParameters property is true.

In a generic type definition, a placeholder for a type that will be assigned later.

generic type argument

Can be any type, including a generic type parameter.

Type arguments are specified as an array of Type objects passed to the Type.MakeGenericType(Type[]) method when creating a constructed generic type. If instances of the resulting type are to be created, the Type.ContainsGenericParameters property must be false for all the type arguments.

The following code example and table illustrate some of these terms and invariants. The Derived class is of particular interest because its base type is a constructed type that has a mixture of types and type parameters in its type argument list.

code reference: System.Type.IsGenericType#2

The following table shows examples that use and build on the classes Base, Derived, and G. When the C++ and C# code is the same, only one entry is shown.

Derived(Of V)

Derived<V>

For this type:

Type.IsGenericType is true.

Type.IsGenericTypeDefinition is true.

Type.ContainsGenericParameters is true.

Base(Of String, V)

Base<String,V>

Base<String^,V>

For this type:

Type.IsGenericType is true.

Type.IsGenericTypeDefinition is false.

Type.ContainsGenericParameters is true.

Dim d() As Derived(Of Integer)

Derived<int>[] d;

array<Derived<int>^>^ d;

For the type of variable d:

Type.IsGenericType is false because d is an array.

Type.IsGenericTypeDefinition is false.

Type.ContainsGenericParameters is false.

T, U, and V (everywhere they appear)

Type.IsGenericParameter is true.

Type.IsGenericType is false because there is no way to constrain a type parameter to generic types.

Type.IsGenericTypeDefinition is false.

Type.ContainsGenericParameters is true because T, U, and V are themselves generic type parameters. This does not imply anything about type arguments that are assigned to them later.

The type of field F

Type.IsGenericType is true.

Type.IsGenericTypeDefinition is false because a type has been assigned to to the type parameter of G. Note that this is equivalent to having called the Type.MakeGenericType(Type[]) method.

Type.ContainsGenericParameters is true because the type of field F has a type argument that is an open constructed type. The constructed type is open because its type argument (that is, Base) is a generic type definition. This illustrates the recursive nature of the Type.IsGenericType property.

The nested class Nested

Type.IsGenericType is true, even though the Nested class has no generic type parameters of its own, because it is nested in a generic type.

Type.IsGenericTypeDefinition is true. That is, you can call the Type.MakeGenericType(Type[]) method and supply the type parameter of the enclosing type, Derived.

Type.ContainsGenericParameters is true because the enclosing type, Derived, has generic type parameters. This illustrates the recursive nature of the Type.ContainsGenericParameters property.

Example

For an example of using this method, see the example for Type.MakeGenericType.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0