I recently stumbled upon a little known operator in C#, that is pretty slick. It's the "??"
operator. Yeah, that's right, it's just 2 question marks. Unfortunately it's nearly impossible
to find anything in Google about it, since they don't let you search for characters.
Anyway, this operator is great because it's basically like the SQL IsNull or Coalesce. You can
take multiple values, and get the first non-null value.
For example:
object a, b, c, d;
b = null;
c = null;
d = new object();
a = b ?? c ?? d;
Assert.AreEqual(a, d);
I wish I had known about this earlier, it's pretty cool.
For more information, here is the direct MSDN documentation
about it.
Also, I'm going to try to list some search terms for anyone looking for information about this.
Hopefully Google will index them:
question mark question mark
double question mark
.NET 2.0 coalesce
question mark operator
posted @ Thursday, June 21, 2007 9:50 AM