null coalescing大致翻译为 return x, unless it is null, in which case return y
我经常需要 return null if x is null, otherwise return x.y
我可以用 return x == null ? null : x.y;
不错,但null中间总是困扰我 - 这似乎是多余的.我更喜欢这样的东西return x :: x.y;,::只有在它之前的东西不是的时候才会评估null.
我认为这几乎与null合并相反,有点简洁,内联null检查,但我[ 几乎 ]确定在C#中没有这样的运算符.
(我知道我可以用C#编写一个方法;我使用return NullOrValue.of(x, () => x.y);,但如果你有更好的东西,我也希望看到它.)
c# syntax programming-languages operators null-coalescing-operator