nullable value types in C# 2005

J

John Wood

I was just looking at an article about using nullable value types. (value
types that can effectively have no value and not be set).

The syntax is to append a question-mark to the value type in the
declaration, eg:

int? age;

I don't like that much, I think it would be much more consistent to use a
new keyword, such as "nullable". But anyways...

A couple of questions for anyone who actually has the beta installed:

1. What's the overhead of declaring your value types nullable?
2. Are they type-compatible with non-nullable value types? (ie. can I assign
one to the other?).

Thanks,
John
 
G

Guest

1. What's the overhead of declaring your value types nullable?

They are implemented as an instance of the library struct Nullable, so int?
2. Are they type-compatible with non-nullable value types? (ie. can I assign
one to the other?).

From int to int? there is an implicit conversion.

In the other direction is it explicit (i.e. you need to cast) since it might
fail (i.e. the int? could be null which is a value the int cannot handle).

There is a lot more to the type conversion story with nullable types, but
that's probably enough for the moment unless you have some specific question
in mind.

Mark
 
R

Richard Blewett [DevelopMentor]

When you use say

int? i = null;

what you are really saying is

Nullable<int> i = null;

Now Nullable<T> is a value type generic. It contains the value (in this case an int) and a boolean flag to state whether the value has been set or not. So in terms of overhead you are simply adding a boolean flag to the structure.

As far as compatibility to teh non-nullable types, you can write

int? i = 5;
if( i.HasValue )
{
int j = (int)i;
int k = i.Value;
}

both of these constructs work. However, if you do not perform the check first and simply try to cast or assign the Value - and the nullable type is null - then you will get an InvalidOperationException

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I was just looking at an article about using nullable value types. (value
types that can effectively have no value and not be set).

The syntax is to append a question-mark to the value type in the
declaration, eg:

int? age;

I don't like that much, I think it would be much more consistent to use a
new keyword, such as "nullable". But anyways...

A couple of questions for anyone who actually has the beta installed:

1. What's the overhead of declaring your value types nullable?
2. Are they type-compatible with non-nullable value types? (ie. can I assign
one to the other?).

Thanks,
John
 
S

Shawn B.

What about whether I want to assign it to null again for some reason, there
are such cases. Is there a way to reset the flag to false? Or once it is
set, it is permanent? I can always test it myself but I am not at my PC at
home so I ask.


Thanks,
Shawn


Richard Blewett said:
When you use say

int? i = null;

what you are really saying is

Nullable<int> i = null;

Now Nullable<T> is a value type generic. It contains the value (in this
case an int) and a boolean flag to state whether the value has been set or
not. So in terms of overhead you are simply adding a boolean flag to the
structure.
As far as compatibility to teh non-nullable types, you can write

int? i = 5;
if( i.HasValue )
{
int j = (int)i;
int k = i.Value;
}

both of these constructs work. However, if you do not perform the check
first and simply try to cast or assign the Value - and the nullable type is
null - then you will get an InvalidOperationException
 
R

Richard Blewett [DevelopMentor]

int? i = 20;
if( i.HasValue )
{
Console.WriteLine(i.Value);
}
i = null;
if( i.HasValue )
{
Console.WriteLine(i.Value);
}
else
{
Console.WriteLine("null");
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

What about whether I want to assign it to null again for some reason, there
are such cases. Is there a way to reset the flag to false? Or once it is
set, it is permanent? I can always test it myself but I am not at my PC at
home so I ask.


Thanks,
Shawn
 
J

Jon Skeet [C# MVP]

Shawn B. said:
What about whether I want to assign it to null again for some reason, there
are such cases. Is there a way to reset the flag to false? Or once it is
set, it is permanent? I can always test it myself but I am not at my PC at
home so I ask.

I believe you can set it to null or non-null as many times as you like,
just as you would a reference type.
 
J

John Wood

My question really is - is it a reference type under the hood? Is it
basically a way of declaring a value type that's boxes by default? Or does
it declare a ghost flag variable that indicates whether it's null, and hide
that? Or is it more clever than that?
 
J

Jon Skeet [C# MVP]

John Wood said:
My question really is - is it a reference type under the hood?

No. It's a value type which consists of the "normal" type (eg int) and
a bool.
Is it
basically a way of declaring a value type that's boxes by default? Or does
it declare a ghost flag variable that indicates whether it's null, and hide
that? Or is it more clever than that?

The "ghost flag" bit, basically. At least, that's the way I understand
it.
 
J

John Wood

I doubt it works that way actually... because what would you do for
parameters -- pass in a null flag for each nullable value type also?

Some people theorize that it just creates a struct type for any nullable
value type (maybe using generics), that includes the value type and a flag.
It can then generate type converters to make it compatible with the original
type.
 
J

Jon Skeet [C# MVP]

John Wood said:
I doubt it works that way actually... because what would you do for
parameters -- pass in a null flag for each nullable value type also?

Not sure what you mean. Could you elaborate?
Some people theorize that it just creates a struct type for any nullable
value type (maybe using generics), that includes the value type and a flag.

Yes, that's exactly what I was describing...
It can then generate type converters to make it compatible with the original
type.

Exactly.
 
R

Richard Blewett [DevelopMentor]

No, Jon is spot on

int? is the same as the value type generic Nullable<int>

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I doubt it works that way actually... because what would you do for
parameters -- pass in a null flag for each nullable value type also?

Some people theorize that it just creates a struct type for any nullable
value type (maybe using generics), that includes the value type and a flag.
It can then generate type converters to make it compatible with the original
type.
 

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