The C# ?? null coalescing operator
A simple tip to save typing and increase your codes readability is the "double question mark operator", more accurately called the "null coalescing operator".
Instead of using this to set defaults in a function
function void test(int aVar) { int myVar = aVar; if (aVar == null) { myVar = 42; } }
or
function void test(int aVar) { int myVar = aVar == null ? 42 : aVar; }
You can simply use
function void test(int aVar) { int myVar = aVar ?? 42; }
Granted, you'd likely find much better ways to use this than simply defaults in a function call, but hey, this is just an example, let your imagination do the work.
MSDN reference: http://msdn.microsoft.com/en-us/library/ms173224.aspx
Written on June 11, 2009