Loading user information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading user information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

?? not only a way to say I'm confused

Scott Guthrie has a great post about question marks and LINQ.  Well, actually, it is about the c# null coalescing operator, ??.  So what does the ?? operator do?  It checks a value and if null, put in something different.

Old way:

string foo = arg[0];
if( foo == null)
    foo = "bar";

A new possible way using ??:

string foo = arg[0] ?? "bar";

What is even neater is you can chain values together to provide fallback.

string item = DBValue ?? ConfigValue ?? "SomeOtherValue";

Scott then goes on and demonstrates how to use this operator with LINQ.  He also promised to show how to do something similar with the LINQ to XML Literal support in VB.


Follow the discussion

  • Oops, something didn't work.

    Getting subscription
    Subscribe to this conversation

    This is not new.  It's been in C# since at least .NET 2.0, maybe longer.

  • MichaelMichael

    What I'm missing is a complementary version to make it easier to write:

    string foo = (bar == null) ? null : Foo(bar);

    But it's hard to not laugh outright at the obvious choice for it:

    string foo = bar !? Foo(bar)

Remove this comment

Remove this thread


Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.