Welcome to MSDN Blogs Sign in | Join | Help

Rexiology@MSDN

Nothing but Microsoft Technologies...
C#: What does it mean about statement "int? varA = 3;" ?

Ok, I have to admit that I didn't really go through the whole C# language reference and today when I was reading some code I got confused with the follow syntax:

int? varA = 3;

what's the "?" means in this statement? don't have this in earlier C# specifications. An internet search found out the answer that according to this post it is a shortcut of Nullable Type definition:

Nullable<Int> varA = 3; // or
Nullable<T> variable; // is equal to T? variable;

Agreed to what Justin Rogers said that it easily confuse programmer if one didn't read most of the language definitions (well, I do think it is basic and necessary to read all the language definition before starting to using a programming language).

Also there is (new?) operator "??" that using like this:

int? varA = null;
int varB = 3;
int? varC = 4;
int result1 = varA ?? varB; // will return varB = 3 since varA is null
int result2 = varB ?? varC; // will return varB = 3 since varB is not null

the "??" operator is to check if the left-hand operand is null. if the left-hand operand is not null than it return left-hand operand, or else it return right-hand operand. see the ?? definition here.

So now we know that the "?" means a Nullable type in C# 2.0 and ?? is to check null values. (maybe it's only me that don't know about it) 

FYI.

Posted: Tuesday, July 03, 2007 9:46 PM by rextangtw
Filed under:

Comments

Dave said:

You can think of ?? as C#'s equivalent of isnull or coalesce.  Once you get used to using it, it's hard to imagine how you ever got along without it.

I'm a big fan of int? too.  "if (Int.HasValue)" is great for readability.

# July 3, 2007 10:14 PM

mcgurk said:

I wanted to be a big fan of nullable primitive types, but since the only reason why you would ever want them--database access--fails to use them, I never touch 'em.  Lots of people were shocked when we saw that ADO wasn't reworked to use nullables and we were still stuck with the monstrous "if(!reader.IsDBNull(0)) i = (int)reader[0];".  If they had just implemented something as simple as "int? i = reader[0]" nullable types would be in widespread use now.  Unfortunately, they didn't, and nullables are collecting dust.

# July 5, 2007 1:43 PM

Richard said:

mcgurk: You can write "int? i = reader[0] as int?", which is a lot nicer than "if (!reader.IsDbNull...".

# July 13, 2007 9:17 AM

Derrick said:

I'm kind of new to C#, and still somewhat considered a novice when it comes to programming in general, but. . .   Doesn't isnull only return a boolean value whereas ?? would return something else (an object reference?)?

# July 9, 2008 2:07 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker