Null-conditional operator and !=

前端 未结 3 1112
温柔的废话
温柔的废话 2020-12-31 01:39

With the introduction of Null-Conditional Operators in C#, for the following evaluation,

if (instance != null && instance.Val != 0)

3条回答
  •  Happy的楠姐
    2020-12-31 02:11

    You could make use of null coallescing operator:

    instance?.Val ?? 0
    

    When instance is null or instance is not null but Val is, the above expression would evaluate to 0. Otherwise the value of Val would be returned.

提交回复
热议问题