在C#中有更简单的方法吗?(null-coalescing type question)

Ste*_*uiz 3 c# null if-statement null-coalescing-operator

有更简单的方法吗?

string s = i["property"] != null ? "none" : i["property"].ToString();
Run Code Online (Sandbox Code Playgroud)

注意它和null-coalesce(??)之间的区别是在返回之前访问not-null值(op的第一个操作数).

Jar*_*Par 6

请尝试以下方法

string s = (i["property"] ?? "none").ToString();
Run Code Online (Sandbox Code Playgroud)