我已经看过几次以下的东西......而且我讨厌它.这基本上是"欺骗"的语言吗?或者..你会认为这是'ok',因为IsNullOrEmpty会一直被评估吗?
(我们可以争论一个字符串在函数出现时是否应该为NULL,但这不是问题.)
string someString;
someString = MagicFunction();
if (!string.IsNullOrEmpty(someString) && someString.Length > 3)
{
// normal string, do whatever
}
else
{
// On a NULL string, it drops to here, because first evaluation of IsNullOrEmpty fails
// However, the Length function, if used by itself, would throw an exception.
}
Run Code Online (Sandbox Code Playgroud)
编辑: 再次感谢大家提醒我这种语言的基础.虽然我知道"为什么"它起作用,但我无法相信我不知道/记住这个概念的名称.
(如果有人想要任何背景..我在解决由NULL字符串和.Length> x例外...在代码的不同位置生成的异常时遇到此问题.所以当我看到上面的代码时,除了其他所有内容之外,我的挫败感从那里开始.)