Microsoft, Information Technologies...



  • From Taiwan, living and working at Tokyo, Japan.

Recent Posts


Microsoft Sites

Other Sites

Blog pools



Site Info

Locations of visitors to this page

Logos & Chicklets


Rex's Certifications
Rex's Certifications

Creative Commons授權條款
本 著作 係採用
Creative Commons 授權條款

C#: What does it mean about statement "int? varA = 3;" ?

crosspost from

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) 


Posted: 2007/7/3 20:47 by rextangtw | with no comments
Filed under: