2011-11-16 63 views
6

我想为if语句创建一个表达式查询,如果test为null,访问器会导致错误。如果没有别的语句

test != null ? test.Contains("mystring") : NO_VLAUE 

我要找:

test != null ? test.Contains("mystring") 

otherwise ignore. 

我知道我可以使用一个??is null但有一个逆。

在此先感谢。

+0

空串可能吗? –

回答

8

这听起来像你想test != null && test.Contains("mystring")

你的问题问的没有意义。所有表达式,包括条件运算符,都必须有一个值。如果test为空,您希望表达式能评估什么?

如果test为null,您可能希望它为false。
换句话说,如果test不为null并且它包含mystring,则希望它为true。

7

这听起来像你可能想:

test != null && test.Contains("mystring") 

这将评估为false如果test为空 - 是你想要的吗?基本上你需要说明你想要发生什么,如果test null,否则它不能用作表达式。