Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否有一条语句或一条线的方式来完成这样的事情,其中字符串 s 被声明并分配了表达式中的第一个非空值?
//pseudo-codeish string s = Coalesce(string1, string2, string3);
或者,更一般地说,
object obj = Coalesce(obj1, obj2, obj3, ...objx);
正如达伦·科普所说。
你的陈述
可以这样写:
object obj = obj1 ?? obj2 ?? obj3 ?? ... objx;
换句话说:
var a = b ?? c;
相当于
var a = b != null ? b : c;
?? _ 操作员。
string a = nullstring ?? "empty!";