473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

nullable value types in C# 2005

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
Nov 16 '05 #1
10 1796
> 1. What's the overhead of declaring your value types nullable?

They are implemented as an instance of the library struct Nullable, so int?
is really just Nullable<int>. Inside, there is a bool to record
defined/undefined state and the int to store the data.
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
Nov 16 '05 #2
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

Nov 16 '05 #3
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 [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:O2**************@TK2MSFTNGP15.phx.gbl...
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

Nov 16 '05 #4
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

Nov 16 '05 #5
Shawn B. <le****@html.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
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?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Shawn B. <le****@html.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7
John Wood <john.wood@priorganize_nospam__.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
John Wood <john.wood@priorganize_nospam__.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #9
John Wood <john.wood@priorganize_nospam__.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
John Wood <john.wood@priorganize_nospam__.com> wrote:
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.


Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ESPNSTI | last post by:
Hi, Please don't shoot me if the answer is simple, I'm new to .Net and C# :) .. I'm attempting to convert a nullable type to it's "non-nullable" type in a generic way (without knowing what...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
12
by: Steven Livingstone | last post by:
I've just blogged some stuff on Nullable types in net 2.0. http://stevenr2.blogspot.com/2006/01/nullable-types-and-null-coalescing.html Question however as to why you can't simply get an implcit...
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
0
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
3
by: Mike P | last post by:
What are nullable types used for? The only use I can think of is where I have a method like this where some values being passed to it may be null : public int AddUser(int? UserRegion, string...
6
by: Tony Johansson | last post by:
Hello! I'm reading in a book called Visual C# 2005. In the chapter about Generics there ia a section about Nullable types. Here is the text that isn't complete true I think. It says: "You...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.