How the right associative of null coalescing operator behaves?

后端 未结 4 1493
Happy的楠姐
Happy的楠姐 2020-12-03 01:20

null coalescing operator is right associative, which means an expression of the form

first ?? second ??third

is eva

4条回答
  •  长情又很酷
    2020-12-03 01:55

    Both work as expected and are effectively identical because the expressions involve simple types (thank you @Jon Skeet). It will chain to the first non-null from left to right in your examples.

    The precedence (thanks @Ben Voigt) is more interesting when combining this operator with operators of different precedence:

    value = A ?? B ? C : D ?? E;
    

    Basically, associativity is expressed inherently through operator precedence or through user introduced sub-expressions (parentheses).

提交回复
热议问题