Nullable Types in C# 2.0

I'm teaching a C# 2.0 class at Interface Technical Training this week and thought I'd blog about two features I really like in C# 2.0 that save some typing (although they may arguably lead to more cryptic code) and are really useful.  Sure, there's all the goodness associated with generics, anonymous methods, partial types, etc. in C# 2.0 but two nice language enhancements are nullable types (which relate to generics) and the ?? operator.  If you've ever retrieved a null value from a database and then tried to assign it to an int or DateTime variable then you'll definitely appreciate nullable types if you haven't already used them.

Nullable types can be defined in different ways. The first is using the Nullable<T> struct which is new in .NET 2.0:

//Call a DB routine that may return a C# null value (not DBNull.Value) 
Nullable<
int> age GetAge()

While this works, I prefer the following syntax which is the same as the code above but more compact:

//Call a DB routine that may return a C# null value (not DBNull.Value)
int
? age GetAge()

I also like the ?? operator since it provides a compact way to check if a variable is null.  If the value is null then a default value can be returned.  Instead of the writing the following code to check for nulls (which uses short-cut conditional logic):

int? age = GetAge();
int
? tempAge (age==null)?-1:age;

I could instead use the ?? operator:

int? age = GetAge();
int? 
tempAge age??-1;

The code that uses the ?? operator says that if age isn't null then assign it to tempAge.  If age is null then assign tempAge a value of -1.  Some may argue that this makes code too cryptic (and they'd have a point).  But, I like compact coding solutions.  Besides....it's not nearly as bad as writing regular expressions. :-)

 

comments powered by Disqus

7 Comments

  • Is ?: really using conditional logic? Meaning:

    a == b and c or d

    is the same as:

    a == b ? c : d

    I not where I can check, but with the following conditional logic version you would get an unexpected result:

    1 == 1 ? false : true

    If the ? is an "and", then the statement is false and the return is "true" - though that is not what is desired.

    I had always assumed ?: in C# and other languages was a short hand if/else statement.

  • Haven't had many comments where I had to put some thought into it...glad you posted. :-)

    ?: is a short-hand (short circuit) if/else statement and ultimately a conditional. The C# SDK defines ?: in the following way:

    "The conditional operator (?:) returns one of two values depending on the value of a Boolean expression.

    condition ? first_expression : second_expression;

    If condition is true, first expression is evaluated and becomes the result; if false, the second expression is evaluated and becomes the result. Only one of two expressions is ever evaluated."

    bool test = (1==1)?false:true; would be backwards since if 1==1 (which it does of course) then the first_expression should return the true condition. In that case the false condition would never get hit though...in fact the compiler will let you know that the false condition is unreachable. In a more realistic situation like the following:

    int x = 1;
    bool test = (x==1)?true:false;

    the compiler doesn't know which expression will be returned and won't mark the "false" expression as unreachable. But, since x == 1 here it will never be evaluated anyway in this scenario.

  • Nullable types would be a lot more useful if they mapped to DbNull.

  • I agree. Maybe next version... But, it's still a lot better than what we had in V1.

  • I've had a chance to check out everything in test code, and it works as expected. I think I had a different definition of short-circuit than you did, which led to the question. I have always used short-circuit to mean if the first part of an AND is false, the second part is not evaluate; also if the first part of an OR is true, the second part is not evaluated.

    In Python (where there wasn't a ?: operator until 2.5) we often used x = cond and true or false - which did rely upon the short-circuit as I described it above. However if the "true" was meant to be a false value (empty, zero, etc) it would break.

    BTW, thanks for the post - I wish I knew of ?? earlier!

  • No problem. &nbsp;Actually, I probably shouldn&#39;t have used the term &quot;short circuit&quot; since I traditionally think of it the same way as you mentioned.&nbsp;&nbsp;

  • Yes Very good Blog. I knew about the Nullable types but not about "?"

Comments have been disabled for this content.