Export (0) Print
Expand All
Expand Minimize

Compiler Error CS0403

Cannot convert null to type parameter 'name' because it could be a non-nullable value type. Consider using default('T') instead.

You cannot assign null to the unknown type named because it might be a value type, which does not allow null assignment. If your generic class is not intended to accept value types, use the class type constraint. If it can accept value types, such as the built-in types, you may be able to replace the assignment to null with the expression default(T), as shown in the following example.

The following sample generates CS0403.

// CS0403.cs
// compile with: /target:library
class C<T>
{
   public void f()
   {
      T t = null;  // CS0403
      T t2 = default(T);   // OK
    }
}

class D<T> where T : class 
{
   public void f()
   {
      T t = null;  // OK
    }
}

Community Additions

ADD
Show:
© 2015 Microsoft