Type.IsGenericParameter Property

Definition

Gets a value indicating whether the current Type represents a type parameter in the definition of a generic type or method.

C#
public abstract bool IsGenericParameter { get; }
C#
public virtual bool IsGenericParameter { get; }

Property Value

true if the Type object represents a type parameter of a generic type definition or generic method definition; otherwise, false.

Examples

The following example uses the IsGenericParameter property to test for generic type parameters in a generic type.

C#
if (t.IsGenericType)
{
    // If this is a generic type, display the type arguments.
    //
    Type[] typeArguments = t.GetGenericArguments();

    Console.WriteLine("\tList type arguments ({0}):", 
        typeArguments.Length);

    foreach (Type tParam in typeArguments)
    {
        // If this is a type parameter, display its
        // position.
        //
        if (tParam.IsGenericParameter)
        {
            Console.WriteLine("\t\t{0}\t(unassigned - parameter position {1})",
                tParam,
                tParam.GenericParameterPosition);
        }
        else
        {
            Console.WriteLine("\t\t{0}", tParam);
        }
    }
}

Remarks

Type objects that represent generic type parameters can be obtained by calling the GetGenericArguments method of a Type object that represents a generic type definition, or the GetGenericArguments method of a MethodInfo object that represents a generic method definition.

  • For a generic type or method definition, the IsGenericParameter property returns true for every element of the resulting array.

  • For a closed constructed type or method, the IsGenericParameter property returns false for every element of the array returned by the GetGenericArguments method.

  • For an open constructed type or method, some elements of the array might be specific types and others might be type parameters. IsGenericParameter returns false for the types and true for the type parameters. The code example for the ContainsGenericParameters property demonstrates a generic class with a mixture of types and type parameters.

For a list of the invariant conditions for terms used in generic reflection, see the IsGenericType property remarks.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also