20090421

C# ?? Operator

Rather than write something awkward like this:
if (myList != null)
{
 return myList;
}
else
{
 return null;
}
Try using the ?? operator instead:
return myList ?? null;
Much more elegant, n'est pas?

2 comments:

  1. Don't forget the name: null coalescing operator.

    The operator can be chained as well:

    string setting = userValue ?? configValue ?? "some default";

    ReplyDelete