Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>'

Tags:

c#

generics

I'm using .net framework 4.5

I get the following error

Error CS0453 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'

  public async Task<Nullable<MyObject>> MyMethod(string myParamter)
    {}

I also tried

  public async Task<MyObject?> MyMethod(string myParamter)
    {}

If I set the thing as nullable then why do I get the red line below the method name with this error message

The stackoverflow answer is easy, make the return type nullable but for me Visual studio doesn't allow it.

like image 593
devfric Avatar asked Feb 26 '16 09:02

devfric


1 Answers

Since MyObject is an object, and objects are nullable by definition, there is no use to use the Nullable<T> struct or ? operator in order to support null values.

Nullable<T> is for structs (like DateTime) and value types like int, float, etc.

like image 109
Patrick Hofman Avatar answered Oct 03 '22 05:10

Patrick Hofman