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

davy

I have the need to make a class that can hold the value for the following types: int?, decimal?, date, bool? string.

I want to be able to do soemthing like:

var x = new MyClass<int?>()  
x.Value = null;
x.value = 99;         

// or

var y = new MyClass<bool?>();
y.Value = null;
y.Value = true

// and so on

I have been trying to create a class of type T along the lines of:

public class TestClass<T>
{             
    public T? Value{ get; set; }
}

I want to use Value to take any of the allowed types but I can't make T nullable. The error is:

Only non nullable value type could be underlying of Sysytem.Nullable

is there anyway of doing what I'm trying to achieve?

gzaxx

Try this:

public class TestClass<T> where T : struct
{             
    public T? Value{ get; set; }
}

Classes cannot be nullable as class is a reference type. You have to add constraint to your generic class to allow only structs (which can be nullable).

string is a reference type and your class won't work with string it this case as it can't be nullable.

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

The type 'T' is not compatible with the type 'T'

From Dev

Cast generic type T into Class<T>

From Dev

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

From Dev

Why is class of T not allowed in a generic type?

From Dev

Class type required but T found

From Dev

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

From Dev

How does the compiler recognise Nullable<T> to be a 'special type'?

From Dev

Constraints on type parameters - where T: class

From Dev

TypedQuery<T>.getSingleResult returns object of type Class instead of type T

From Dev

Scala: class type required but T found

From Dev

Rspec 3: can't check class type

From Dev

Jackson DeSerialize Class with <T> (Generic) Type

From Dev

IEnumerable<T> unknown type preserve class information

From Dev

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

From Dev

'type' is not a member type of 'T'

From Dev

Scala types: Class A is not equal to the T where T is: type T = A

From Dev

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

From Dev

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

From Dev

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

From Dev

how to instantiate T type inside a template class

From Dev

Why is class of T not allowed in a generic type?

From Dev

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

From Dev

Scala: class type required but T found

From Dev

IEnumerable<T> unknown type preserve class information

From Dev

'type' is not a member type of 'T'

From Dev

Generic Class:: Type Parameter <T> Property Inheritance

From Dev

Get type of Class from generic Class(of T)

From Dev

TypeScript guard for nullable type doesn't work

Related Related

  1. 1

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

  2. 2

    The type 'T' is not compatible with the type 'T'

  3. 3

    Cast generic type T into Class<T>

  4. 4

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

  5. 5

    Why is class of T not allowed in a generic type?

  6. 6

    Class type required but T found

  7. 7

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

  8. 8

    How does the compiler recognise Nullable<T> to be a 'special type'?

  9. 9

    Constraints on type parameters - where T: class

  10. 10

    TypedQuery<T>.getSingleResult returns object of type Class instead of type T

  11. 11

    Scala: class type required but T found

  12. 12

    Rspec 3: can't check class type

  13. 13

    Jackson DeSerialize Class with <T> (Generic) Type

  14. 14

    IEnumerable<T> unknown type preserve class information

  15. 15

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

  16. 16

    'type' is not a member type of 'T'

  17. 17

    Scala types: Class A is not equal to the T where T is: type T = A

  18. 18

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

  19. 19

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

  20. 20

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

  21. 21

    how to instantiate T type inside a template class

  22. 22

    Why is class of T not allowed in a generic type?

  23. 23

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

  24. 24

    Scala: class type required but T found

  25. 25

    IEnumerable<T> unknown type preserve class information

  26. 26

    'type' is not a member type of 'T'

  27. 27

    Generic Class:: Type Parameter <T> Property Inheritance

  28. 28

    Get type of Class from generic Class(of T)

  29. 29

    TypeScript guard for nullable type doesn't work

HotTag

Archive