带有null检查的c#三元操作符行为不正常



c#三元运算符null检查不正常

"City updated from old value '" + isOtherAccountExist == null ? "EMPTY" :isOtherAccountExist.City + "' to ' something else"

在上面的语句中,期望是如果isOtherAccountExist=null,那么它应该简单地返回而不是检查isOtherAccountExist.City值。但它检查并抛出NullReference异常。

系统。NullReferenceException: '对象引用未设置为an对象的实例。'

string isOtherAccountExist = "";
string value = isOtherAccountExist == null ? "EMPTY" : isOtherAccountExist;
Console.WriteLine(
$"City updated from old value {value} to something else");

最新更新