Saturday, February 4, 2012

C# "??" operator

I just ran across something I hadn't seen before - a two question mark operator. I found it in the AccountMembership service method in the default AccountModel.cs file that's used when you start a new ASP.NET MVC 3 project. Here's the line of code:
_provider = provider ?? Membership.Provider;
I did a little research and found it's just a shortcut for this, using an iif/conditional:
_provider = provider != null ? provider : Membership.Provider;
which of course is a shortcut for this regular if statement:
if(provider != null) {
     _provider = provider;
} else {
     _provider = Membership.Provider;

More info here:
Very cool. I love the terseness of C#.

No comments: