Possible Duplicate:
Nullable type as a generic parameter possible?
I came across a very weird thing with generic type constraints. I have a class like this:
public SomeClass<T> where T:class
{
}
However, I've found I can't use nullable types as I'd expect:
new SomeClass<int?>();
I get an error that int?
must be a reference type. Is Nullable really just a struct with syntactic sugar to make it look like a reference type?
public struct Nullable<T> where T : struct
– Physicalityint? x = null
, but you can't do that with any other value type.. so I need a restriction to something likewhere T:class, T:Nullable
, but that doesn't seem to work – Gargantuanull
and anyNullable
value. It doesn't actually store null, it just has a booleanHasValue
and assigning null to it results in that being false and the actualValue
property being...anything. This means that putting a value into a nullable struct doesn't actually box it or result in it gaining reference semantics. This does need special compiler support as there's no way to have a custom type with an implicity conversion for a struct fromnull
while also not allowing any actual objects to be converted with it. – Aunt