int? question

  • Thread starter Thread starter ^MisterJingo^
  • Start date Start date
M

^MisterJingo^

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
 
^MisterJingo^ said:
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
 
Thanks for the reply. Out of curiosity, in what circumstances would you
want to assign null to value types?

Chris
 
sql queries results.
You could have int column and for any rows the value could be null.
 
edi said:
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
 
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 said:
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]
- (e-mail address removed)

^MisterJingo^ said:
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
 
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/
 
Back
Top