在构建表达式查询时,我正在尝试使用 if 语句的简写形式,如果 test 为 null,则访问器会导致错误。
test != null ? test.Contains("mystring") : NO_VLAUE
我正在寻找:
test != null ? test.Contains("mystring")
otherwise ignore.
我知道我可以使用 ??
来表示 is null
但是否存在相反的情况。
提前致谢。
最佳答案
听起来你想要 test != null && test.Contains("mystring")
您提出的问题没有意义。所有表达式,包括条件运算符,都必须有一个值。如果 test
为 null,您期望该表达式的计算结果是什么?
如果测试为空,您可能希望它为假。
换句话说,如果 test 不为空并且它包含 mystring
,您希望它为真。
关于c# - 没有 else 的简写 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8153481/