471,309 Members | 1,080 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

int? question

I saw this code on the net and was curious if anyone could tell me what
the '?' directly proceding the int does?

int? i = null;
object o = i;
if (o == null)
Console.WriteLine("Correct behaviour - you are running a
version from Sept 05 or later");
else
Console.WriteLine("Incorrect behaviour, prior to Sept 05
releases");

The place I found the code didn't explain, and search for int? on
google brings nothing up. Without the '?' in place null can't be
assigned to a value type (int).
Any help would be appreciated.

Chris

Dec 11 '05 #1
7 1009
^MisterJingo^ wrote:
I saw this code on the net and was curious if anyone could tell me what
the '?' directly proceding the int does?


Thats a new feature of C# v2. ? creates a NullAble type. This way you
can assign null to ints and other value types.

hth,
Max
Dec 11 '05 #2
Thanks for the reply. Out of curiosity, in what circumstances would you
want to assign null to value types?

Chris

Dec 11 '05 #3
sql queries results.
You could have int column and for any rows the value could be null.

Dec 11 '05 #4

edi sol wrote:
sql queries results.
You could have int column and for any rows the value could be null.


That makes sense :). Does declaring a value type with the suffix '?'
change a data types memory usage (or anything else)?

Chris

Dec 11 '05 #5
MisterJingo,

When you use int? it is the equivalent of:

Nullable<int> i = null;

So basically, it's a different type, but the language will treat
nullable types as special (lifting operators, for example).

Another use of nullable types is for the DateTime instance. It has
always frustrated me that I could not say that there is a lack of a date
value (or any other value type really, but for me the DateTime structure was
the most frustrating).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"^MisterJingo^" <mi*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

edi sol wrote:
sql queries results.
You could have int column and for any rows the value could be null.


That makes sense :). Does declaring a value type with the suffix '?'
change a data types memory usage (or anything else)?

Chris

Dec 11 '05 #6

Nicholas Paldino [.NET/C# MVP] wrote:
MisterJingo,

When you use int? it is the equivalent of:

Nullable<int> i = null;

So basically, it's a different type, but the language will treat
nullable types as special (lifting operators, for example).

Another use of nullable types is for the DateTime instance. It has
always frustrated me that I could not say that there is a lack of a date
value (or any other value type really, but for me the DateTime structure was
the most frustrating).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"^MisterJingo^" <mi*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

edi sol wrote:
sql queries results.
You could have int column and for any rows the value could be null.


That makes sense :). Does declaring a value type with the suffix '?'
change a data types memory usage (or anything else)?

Chris


Thanks for the reply nicholas. ALong with some googling, I understand
now :).

Chris

Dec 12 '05 #7
> Does declaring a value type with the suffix '?' change a data types memory usage.

Yes. Internally, it combines the int with a boolean flag to say whether
the value is null or not, so it will be bigger than a regular int.

Nullable types is part of the C# team's efforts to bridge the gap
between relational databases (and other data storage forms) and
programming languages like C#. Look for information about the LINQ
project (coming to C# 3.0) for their future plans in this regard.

You can start reading about LINQ here:

http://msdn.microsoft.com/netframework/future/linq/

Dec 12 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mohammed Mazid | last post: by
3 posts views Thread by Stevey | last post: by
10 posts views Thread by glenn | last post: by
53 posts views Thread by Jeff | last post: by
56 posts views Thread by spibou | last post: by
2 posts views Thread by Allan Ebdrup | last post: by
3 posts views Thread by Zhang Weiwu | last post: by
reply views Thread by rosydwin | last post: by

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.