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

devfric

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.

Patrick Hofman

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Managed record in generic type produce compile time error "Type parameter 'T' must be non-nullable value type"

From Dev

T must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TModel' in the generic type or method

From Dev

Class of type <T> with nullable value of Type<T>

From Dev

Class of type <T> with nullable value of Type<T>

From Dev

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

From Dev

Extension method on Nullable<T> not called unless generic type is set explicitly

From Dev

Generic type nullable and ToString

From Dev

Generic method to return Nullable Type values

From Dev

The type 'T' cannot be used as type parameter 'T' in the generic type or method

From Dev

Can I create a generic method that takes a value type or a reference type but always returns a nullable type

From Dev

Activator.CreateInstance creates value of type T instead of Nullable<T>

From Dev

An optional parameter must be a reference type, a nullable type, or be declared as an optional

From Dev

Why can't I use a nullable tuple for a nullable generic parameter

From Dev

Type cannot be used as type parameter 'T' in the generic type or method - Why?

From Dev

Use of Nullable Type in method gives error

From Dev

Checking if type matches nullable and non nullable versions

From Dev

What does "The type T must be a reference type in order to use it as parameter" mean?

From Dev

How to get a Nullable<T> instance's type

From Dev

TypeScript guard for nullable type doesn't work

From Dev

The type 'short' must be a reference type in order to use it as parameter 'TTargetEntity' in the generic type

From Dev

Cast to value type 'Enum' failed as the materialized value is null. Either the result type's param or query must use nullable type

From Dev

Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

From Dev

Convert a nullable type to its non-nullable type?

From Dev

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

From Dev

C#.NET MVC4 - null entry for parameter of non-nullable type for a method

From Dev

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

From Dev

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

From Dev

Anonymous Type "Nullable object must have a value" Error

From Dev

Why string or object type don't support nullable reference type?

Related Related

  1. 1

    Managed record in generic type produce compile time error "Type parameter 'T' must be non-nullable value type"

  2. 2

    T must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TModel' in the generic type or method

  3. 3

    Class of type <T> with nullable value of Type<T>

  4. 4

    Class of type <T> with nullable value of Type<T>

  5. 5

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

  6. 6

    Extension method on Nullable<T> not called unless generic type is set explicitly

  7. 7

    Generic type nullable and ToString

  8. 8

    Generic method to return Nullable Type values

  9. 9

    The type 'T' cannot be used as type parameter 'T' in the generic type or method

  10. 10

    Can I create a generic method that takes a value type or a reference type but always returns a nullable type

  11. 11

    Activator.CreateInstance creates value of type T instead of Nullable<T>

  12. 12

    An optional parameter must be a reference type, a nullable type, or be declared as an optional

  13. 13

    Why can't I use a nullable tuple for a nullable generic parameter

  14. 14

    Type cannot be used as type parameter 'T' in the generic type or method - Why?

  15. 15

    Use of Nullable Type in method gives error

  16. 16

    Checking if type matches nullable and non nullable versions

  17. 17

    What does "The type T must be a reference type in order to use it as parameter" mean?

  18. 18

    How to get a Nullable<T> instance's type

  19. 19

    TypeScript guard for nullable type doesn't work

  20. 20

    The type 'short' must be a reference type in order to use it as parameter 'TTargetEntity' in the generic type

  21. 21

    Cast to value type 'Enum' failed as the materialized value is null. Either the result type's param or query must use nullable type

  22. 22

    Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

  23. 23

    Convert a nullable type to its non-nullable type?

  24. 24

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

  25. 25

    C#.NET MVC4 - null entry for parameter of non-nullable type for a method

  26. 26

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

  27. 27

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

  28. 28

    Anonymous Type "Nullable object must have a value" Error

  29. 29

    Why string or object type don't support nullable reference type?

HotTag

Archive