22 September 2009

Null Coalescing

I had seen this before, but now it hit me:

?? is an operator in .NET 2.0 and up.

http://weblogs.asp.net/scottgu/archive/2007/09/20/the-new-c-null-coalescing-operator-and-using-it-with-linq.aspx

So all those lines of code that look like this:

int myInt = (myNullableInt.HasValue) ? myNullableInt.Value : 0;

can now look like this:

int myInt = myNullableInt ?? 0;

Hurray!

No comments:

Post a Comment