<分区>
我知道使用 null coalescing operator 的标准方法在 C# 中是设置默认值。
string nobody = null;
string somebody = "Bob Saget";
string anybody = "";
anybody = nobody ?? "Mr. T"; // Returns Mr. T
anybody = somebody ?? "Mr. T"; // Returns "Bob Saget"
但是 ??
还能用来做什么呢?它似乎不如 ternary operator 有用, 除了比以下更简洁和更容易阅读:
nobody = null;
anybody = nobody == null ? "Bob Saget" : nobody; // Returns Bob Saget
鉴于甚至很少有人知道空合并运算符...
您是否将
??
用于其他用途???
是必需的,还是您应该只使用三元运算符(即 大多数人都熟悉)