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) 


Comments (11)

  1. Dave says:

    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.

  2. mcgurk says:

    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.

  3. Richard says:

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

  4. Derrick says:

    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?)?

  5. GP says:

    Thank you! I also didn't know about it…

  6. tom says:

    me too 🙂

  7. Komal says:

    Thanks for explaining the concept in this much detail…:)

  8. Nejib says:

    Thanks for the detailed explanation

  9. Swati Mathur says:

    Thanks for sharing the knowledge

  10. karim says:

    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