An Uncommon C# DooDad

brian's picture

So I came across this post about the uncommon features of C# when I thought I'd add one of my own that I recently learned from the master of HttpContext.Current.Items, none other than Mike Duncan.

It's the good 'ol coalesce operator (??). Yeah, yeah, you've heard of it in SQL and the like, but it's pretty hot in C#. It will return the first non-null item from a group of two or more objects. Here's an example noting the hottest card game ever made!

int? atat = null;
int cat = 1;
int rat = atat ?? 41 + cat;

And of course the outcome is the answer to life, the universe, and everything. Naturally.

RSS

Poll

Are you going to DevConnections Las Vegas November 10-13th, 2008?
Yes
100%
No
0%
What?
0%
Total votes: 1

Readers / Stuff

Add to Google

facebook