Consistency and Null Coalescing Operators

I’ve been trying to be more consistent lately with my blog entries, making at least one a day during the Monday through Friday work week.  Well, that has sort of kind of maybe not really worked out too hot yet.  But as I sat and thought, while reading through some blogs, I stumbled on Stuart Thompson’s blog where he mentions Null-Coalescing Operators.  These suckers are great!

Per the example on his site, what we used to type when checking for nulls appeared something like this:

    1 namespace SomeNamespace

    2 {

    3     public class Coalescing

    4     {

    5         private string emailAddress = string.Empty;

    6         private string parsedValue = string.Empty;


    8         public string CheckForParsedValue()

    9         {

   10             if (parsedValue != null)

   11             {

   12                 emailAddress = parsedValue;

   13             }

   14             else

   15             {

   16                 emailAddress = “(Not provided)”;

   17             }


   19             return emailAddress;

   20         }

   21     }

   22 }

but now will be coded in a super awesome elite looking way of:

   10 string emailAddress = parsedValue ?? “(Not provided)”;

I’m stoked personally, and I think this makes a great tip o’ the day!  Of course it is kind of a “future” tip o’ the day.  Thanks Stuart pointing out this interesting tidbit of future C# coolness.

Smile [:)]