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.
Technorati tags: microsoft, .net framework, .net, c#, programming, language, nullable, type
Comments
Anonymous
July 03, 2007
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.Anonymous
July 05, 2007
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.Anonymous
July 13, 2007
mcgurk: You can write "int? i = reader[0] as int?", which is a lot nicer than "if (!reader.IsDbNull...".Anonymous
July 09, 2008
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?)?Anonymous
June 15, 2009
PingBack from http://einternetmarketingtools.info/story.php?id=14221Anonymous
May 29, 2011
Thank you! I also didn't know about it...Anonymous
August 18, 2012
me too :-)Anonymous
February 24, 2013
Thanks for explaining the concept in this much detail...:)Anonymous
September 10, 2013
Thanks for the detailed explanationAnonymous
June 24, 2014
Thanks for sharing the knowledgeAnonymous
January 24, 2016
hey there im begginnig with the programmation and i want to know what does 'int' mean please and also if you can suggest me some sites to learn i'll be really thankfull!! thank you already