This topic has not yet been rated - Rate this topic

Compiler Error CS0312

Visual Studio 2008

The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. The nullable type 'type1' does not satisfy the constraint of 'type2'.

A nullable type is distinct from its non-nullable counterpart; no implicit reference conversion or identify conversion exists between them. A nullable boxing conversion does not satisfy a generic type constraint. In the example that follows, the first type parameter is a Nullable<int> and the second type parameter is a System.Int32.

To correct this error

  1. Remove the constraint.

  2. In the following example, make the second type argument either int? or object.

The following code generates CS0312:

// cs0312.cs
class Program
{
    static void MTyVar<T, U>() where T : U { }

    static int Main()
    {
        MTyVar<int?, int>(); // CS0312
        return 1;
    }
}

Although a nullable type is distinct from a non-nullable type, various kinds of conversions are allowed between nullable and non-nullable values.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ