On Sep 30, 9:13*am, "Tony Johansson" <t.johans...@logica.comwrote:
Jon skeet answer this question in a previous mail for several days ago if
nullable type is a reference or a value type?
It's a value type.
With the following answer.
It's a struct - otherwise there'd be relatively little value in having
it instead of having explicit access to the boxed types.
But what does the answer actually mean?
It's still a struct, so there's no separate heap object created. In
other words, it's like this:
struct Nullable<Twhere T : struct
{
private T value;
private boolean hasValue;
// Properties, constructor etc
}
An alternative would have been to make a "wrapper class":
class Nullable<Twhere T : struct
{
private T value;
}
where you'd have a genuine null reference instead of a reference to an
instance. However, that then puts more pressure on the GC etc.
There are a few ways in which nullable types aren't like other
structs:
o The null value boxes to a null reference
o You can unbox from a boxed value of the non-nullable type, or from a
null reference
o You can't use it as the type argument for something with a "where
T : struct" constraint
o You can use null to compare/assign the null value of the nullable
type (i.e. hasValue = false)
Jon