C#: What does it mean about statement "int? varA = 3;" ?
crosspost from http://blogs.msdn.com/rextang
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)