Compiler Error CS0312
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 value 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
.
Remove the constraint.
In the following example, make the second type argument either
int?
orobject
.
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 value type is distinct from a non-nullable type, various kinds of conversions are allowed between nullable and non-nullable values.
.NET feedback
.NET is an open source project. Select a link to provide feedback: