Reference types cannot be wrapped in Nullable<T>
due to a where T : struct
constraint on the generic.
The reasons for this constraint are:
- Reference types are already nullable by definition, and
- Nullable is not very space efficient, but more a "logical" nullability.
Nullable<T>
has a bool property HasValue
and a type T
property Value
which contains the actual value-type value.
Even if HasValue == false
(that is, if the nullable wrapped variable is set to null
), you STILL consume the space for the value type as if it was there.
It's logically nullable to allow you to specify optional behavior, but it doesn't save any space. This is very similar to how boost::optional works in C++.