Search code examples
c#nullif-statementnull-coalescing-operator

Is there an easier way to do this in C#? (null-coalescing type question)


Is there an easier way to do this?

string s = i["property"] != null ? "none" : i["property"].ToString();

notice the difference between it and null-coalesce (??) is that the not-null value (first operand of ?? op) is accessed before returning.


Solution

  • Try the following

    string s = (i["property"] ?? "none").ToString();