#212 – Using Several Null-Coalescing (??) Operators in the Same Expression

You can create an expression that contains more than one null-coalescing (??) operator.  Because the ?? operator is left associative, the expression will be evaluated from left to right.

This means that:

    int result = i ?? j ?? k ?? 0;    // i, j and k are all of type int?

is equivalent to:

    int result = ((i ?? j) ?? k) ?? 0;

The end result of chaining ?? operators together is that the result of the final expression is–the value of the first non-null operand.  In our example, if i and k were null, but j had an integer value, result would be assigned the value of j.

 

 

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: