简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM