Cannot convert null to type parameter because it could be a non nullable type when expecting "T?"

MrD at KookerellaLtd

this code complains

public static T? Foo<T>()
{
   return null;
}

with

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

But aren't I telling the compiler it can be null?

If T is int, then I want the answer to be null, not 0.

Guru Stron

Because T is unconstrained in your case, and for unconstrained (neither to struct nor to class) generic type parameters T - T? is handled differently for value types and reference types. From the docs:

  • If the type argument for T is a reference type, T? references the corresponding nullable reference type. For example, if T is a string, then T? is a string?.
  • If the type argument for T is a value type, T? references the same value type, T. For example, if T is an int, the T? is also an int.
  • If the type argument for T is a nullable reference type, T? references that same nullable reference type. For example, if T is a string?, then T? is also a string?.
  • If the type argument for T is a nullable value type, T? references that same nullable value type. For example, if T is a int?, then T? is also a int?.

So for Foo<int> actual signature will be int Foo<int>, hence the error.

If it is suitable for you - constrain T to be either struct or class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot convert null literal to non-nullable reference type

The value 'null' can't be assigned to the parameter type 'Offset' because 'Offset' is not nullable

A value of type '<null>' cannot be used as a default parameter because there are no standard conversions to type 'T'

Why cannot convert null to type parameter T in c#?

dictionary contains a null entry for parameter 'domainType' of non-nullable type

C# Template Function: Cannot do non-virutal member lookup in 'T' because it is a type parameter

Cannot return Null from a non-nullable type function

How to convert nullable variable to non nullable type?

the parameter cannot have a value of null because of its type when declaring variable

Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't

The argument type 'RenderObject?' can't be assigned to the parameter type 'RenderObject' because 'RenderObject?' is nullable and 'RenderObject' isn't

CS8625 Cannot convert null literal to non-nullable reference type warning for API call that may expect null

Using Linq's Where/Select to filter out null and convert the type to non-nullable cannot be made into an extension method

Nullable enum type cannot be assigned to null when used as a generic

The type 'string' must be a non-nullable type in order to use it as parameter T in the generic type or method 'System.Nullable<T>'

The type 'MyObject' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>'

The type must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'

The cast to value type System.Boolean failed because the materialized value is null, result type's generic parameter must use a nullable type

The parameter can't have a value of 'null' because of its type in Dart

@require parameter '#' can't have a value of 'null' because of its type

The parameter 'id' can't have a value of 'null' because of its type?

Convert a nullable type to its non-nullable type?

Convert array from nullable type to non-nullable of same type?

How to implicitly convert nullable type to non-nullable type

The parameter dictionary contains a null entry for parameter 'IDnumber' of non-nullable type 'System.Int32'

'System.Guid' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type

The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't in Text widget

Error: The argument type 'User?' can't be assigned to the parameter type 'User' because 'User?' is nullable and 'User' isn't. *(NullSafety)*

The property 'xxx' on entity type 'xxx' cannot be marked as nullable/optional because the type of the property is 'short' which is not a nullable type