GenericParameterAttributes Enumeration
Collapse the table of content
Expand the table of content

GenericParameterAttributes Enumeration


Describes the constraints on a generic type parameter of a generic type or method.

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

public enum GenericParameterAttributes

Member nameDescription

The generic type parameter is contravariant. A contravariant type parameter can appear as a parameter type in method signatures.


The generic type parameter is covariant. A covariant type parameter can appear as the result type of a method, the type of a read-only field, a declared base type, or an implemented interface.


A type can be substituted for the generic type parameter only if it has a parameterless constructor.


There are no special flags.


A type can be substituted for the generic type parameter only if it is a value type and is not nullable.


A type can be substituted for the generic type parameter only if it is a reference type.


Selects the combination of all special constraint flags. This value is the result of using logical OR to combine the following flags: DefaultConstructorConstraint, ReferenceTypeConstraint, andNotNullableValueTypeConstraint.


Selects the combination of all variance flags. This value is the result of using logical OR to combine the following flags: ContravariantandCovariant.

The members of the GenericParameterAttributes enumeration are divided into two groups, the variance group and the special constraints group. To test a GenericParameterAttributes value for variance flags, first perform a bitwise AND operation with VarianceMask. If the result is None, there are no variance flags. Similarly, use SpecialConstraintMask to test for constraint flags.

The following code example defines a generic type Test with two type parameters. The second type parameter has a base class constraint and a reference type constraint. When the program executes, the constraints are examined using the Type.GenericParameterAttributes property and the Type.GetGenericParameterConstraints method.

using System;
using System.Reflection;

// Define a sample interface to use as an interface constraint.
public interface ITest {}

// Define a base type to use as a base class constraint.
public class Base {}

// Define the generic type to examine. The first generic type parameter,
// T, derives from the class Base and implements ITest. This demonstrates
// a base class constraint and an interface constraint. The second generic 
// type parameter, U, must be a reference type (class) and must have a 
// default constructor (new()). This demonstrates special constraints.
public class Test<T,U> 
    where T : Base, ITest 
    where U : class, new() {}

// Define a type that derives from Base and implements ITest. This type
// satisfies the constraints on T in class Test.
public class Derived : Base, ITest {}

public class Example
    public static void Main()
        // To get the generic type definition, omit the type
        // arguments but retain the comma to indicate the number
        // of type arguments. 
        Type def = typeof(Test<,>);
        Console.WriteLine("\r\nExamining generic type {0}", def);

        // Get the type parameters of the generic type definition,
        // and display them.
        Type[] defparams = def.GetGenericArguments();
        foreach (Type tp in defparams)
            Console.WriteLine("\r\nType parameter: {0}", tp.Name);

            // List the base class and interface constraints. The
            // constraints are returned in no particular order. If 
            // there are no class or interface constraints, an empty
            // array is returned.
            Type[] tpConstraints = tp.GetGenericParameterConstraints();
            foreach (Type tpc in tpConstraints)
                Console.WriteLine("\t{0}", tpc);

    // List the variance and special constraint flags. 
    private static string ListGenericParameterAttributes(Type t)
        string retval;
        GenericParameterAttributes gpa = t.GenericParameterAttributes;
        GenericParameterAttributes variance = gpa & 

        // Select the variance flags.
        if (variance == GenericParameterAttributes.None)
            retval = "No variance flag;";
            if ((variance & GenericParameterAttributes.Covariant) != 0)
                retval = "Covariant;";
                retval = "Contravariant;";

        // Select 
        GenericParameterAttributes constraints = gpa & 

        if (constraints == GenericParameterAttributes.None)
            retval += " No special constraints";
            if ((constraints & GenericParameterAttributes.ReferenceTypeConstraint) != 0)
                retval += " ReferenceTypeConstraint";
            if ((constraints & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
                retval += " NotNullableValueTypeConstraint";
            if ((constraints & GenericParameterAttributes.DefaultConstructorConstraint) != 0)
                retval += " DefaultConstructorConstraint";

        return retval;
/* This example produces the following output:

Examining generic type Test`2[T,U]

Type parameter: T
        No variance flag; no special constraints.

Type parameter: U
        No variance flag; ReferenceTypeConstraint DefaultConstructorConstraint

Universal Windows Platform
Available since 4.5
.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
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft