Type::ContainsGenericParameters Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets a value indicating whether the current Type object has type parameters that have not been replaced by specific types.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

property bool ContainsGenericParameters {
	virtual bool get();

Property Value

Type: System::Boolean

true if the Type object is itself a generic type parameter or has type parameters for which specific types have not been supplied; otherwise, false.

In order to create an instance of a type, there must be no generic type definitions or open constructed types in the type arguments of the type itself, in any enclosing generic types, or in any elements of the type. Another way of saying this is that when examined recursively, the type must contain no generic type parameters.

Since types can be arbitrarily complex, making this determination is difficult. For convenience and to reduce the chance of error, the ContainsGenericParameters property provides a standard way to distinguish between closed constructed types, which can be instantiated, and open constructed types, which cannot. If the ContainsGenericParameters property returns true, the type cannot be instantiated.

The ContainsGenericParameters property searches recursively for type parameters. For example, it returns true for an array whose elements are type A<T> (A(Of T) in Visual Basic), even though the array is not itself generic. Contrast this with the behavior of the IsGenericType property, which returns false for arrays.

For a set of example classes and a table showing the values of the ContainsGenericParameters property, see IsGenericType.

The following example defines a generic class with two type parameters and then defines a second generic class that derives from the first class. The derived class's base class has two type arguments: the first is Int32 and the second is a type parameter of the derived type. The example displays information about these generic classes, including the positions reported by the GenericParameterPosition property.

using namespace System;
using namespace System::Reflection;
using namespace System::Collections::Generic;

// Define a base class with two type parameters.
generic< class T,class U >
public ref class Base {};

// Define a derived class. The derived class inherits from a constructed
// class that meets the following criteria:
//   (1) Its generic type definition is Base<T, U>.
//   (2) It specifies int for the first type parameter.
//   (3) For the second type parameter, it uses the same type that is used
//       for the type parameter of the derived class.
// Thus, the derived class is a generic type with one type parameter, but
// its base class is an open constructed type with one type argument and
// one type parameter.
generic<class V>
public ref class Derived : Base<int,V> {};

public ref class Test
    static void Main()
            L"\r\n--- Display a generic type and the open constructed");
        Console::WriteLine(L"    type from which it is derived.");

        // Create a Type object representing the generic type definition
        // for the Derived type. Note the absence of type arguments.
        Type^ derivedType = Derived::typeid;

        // Display its open constructed base type.

    static void DisplayGenericTypeInfo(Type^ t)
        Console::WriteLine(L"\r\n{0}", t);
        Console::WriteLine(L"\tIs this a generic type definition? {0}",
        Console::WriteLine(L"\tIs it a generic type? {0}", t->IsGenericType);
        Console::WriteLine(L"\tDoes it have unassigned generic parameters? {0}",
        if (t->IsGenericType)

            // If this is a generic type, display the type arguments.
            array<Type^>^typeArguments = t->GetGenericArguments();
            Console::WriteLine(L"\tList type arguments ({0}):",
            System::Collections::IEnumerator^ myEnum = 
            while (myEnum->MoveNext())
                Type^ tParam = safe_cast<Type^>(myEnum->Current);

                // IsGenericParameter is true only for generic type
                // parameters.
                if (tParam->IsGenericParameter)
                        L"\t\t{0}  (unassigned - parameter position {1})", 
                        tParam, tParam->GenericParameterPosition);
                    Console::WriteLine(L"\t\t{0}", tParam);

int main()

/* This example produces the following output:

--- Display a generic type and the open constructed
    type from which it is derived.

        Is this a generic type definition? True
        Is it a generic type? True
        Does it have unassigned generic parameters? True
        List type arguments (1):
                V  (unassigned - parameter position 0)

        Is this a generic type definition? False
        Is it a generic type? True
        Does it have unassigned generic parameters? True
        List type arguments (2):
                V  (unassigned - parameter position 0)

.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top