nullable types is a struct ?

T

Tony Johansson

Hello!

Jon skeet answer this question in a previous mail for several days ago if
nullable type is a reference or 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?

//Tony
 
J

Jon Skeet [C# MVP]

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<T> where T : struct
{
private T value;
private boolean hasValue;

// Properties, constructor etc
}

An alternative would have been to make a "wrapper class":

class Nullable<T> where 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
 
P

Peter Morris

System.Nullable is a ValueType. It is a generic class, so the reference to
its value is not boxed, the value is also stored as a ValueType. In
addition to holding the value it as a "bool HasValue" property that
indicates whether the value has been set or not.


Does that help?
 
J

Jon Skeet [C# MVP]

System.Nullable is a ValueType.  It is a generic class, so the reference to
its value is not boxed, the value is also stored as a ValueType.

Hang on a sec. Just to be pedantic:

System.Nullable is a static class.
System.Nullable<T> is a value type - a generic *struct* (not class).

Personally I think it's a shame that System.Nullable (the class) even
exists, but there we go...

Jon
 
P

Peter Morris

Hang on a sec. Just to be pedantic:

System.Nullable is a static class.
System.Nullable<T> is a value type - a generic *struct* (not class).
<<

You're pedantic, I'm too idle to write <T> :)
 
M

Marc Gravell

It makes a difference, though.

OK, maybe with List/List<T> we can guess - but Action & Action<T> have
very different meanings, for example. Likewise, IEnumerable and
IEnumerable<T> need disambiguating. If I'm feeling lazy, I'll use the C#
syntax - i.e. List<>, Dictionary<,> etc.

Marc
 
P

Peter Morris

I'm not saying it doesn't matter. I am just saying that I often type a bit
vaguely when I think it's okay, and that the real meaning is easily inferred
based on what has been said previously. Personally I didn't know that
System.Nullable is a static class, I'm not pretending I knew, I've never
used it. I just meant System.Nullable<T> and was being lazy when I typed
:)


Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top