我不喜欢这...这是欺骗的语言? [英] I don't like this... Is this cheating the language?

查看:132
本文介绍了我不喜欢这...这是欺骗的语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到类似下面的几次......我恨它。难道这基本上是'欺骗'的语言?或者..你会认为这是'OK',因为IsNullOrEmpty首先计算,所有的时间?

I have seen something like the following a couple times... and I hate it. Is this basically 'cheating' the language? Or.. would you consider this to be 'ok' because the IsNullOrEmpty is evaluated first, all the time?

(我们可以说一个字符串是否不应为NULL出来时功能,但这并不是真正的问题)

(We could argue whether or not a string should be NULL when it comes out of a function, but that isn't really the question.)

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.
}



编辑:
再次感谢每个人都提醒我这种语言根本。虽然我知道为什么它的工作,我真不敢相信,我不知道/记得概念的名称。

Thanks again to everyone for reminding me of this language fundamental. While I knew "why" it worked, I can't believe I didn't know/remember the name of the concept.

(如果任何人想要的任何背景。我来到经空字符串和。长度系列> X异常产生此故障排除时...例外的代码不同的地方所以当我看到上面的代码中,除了一切,我的无奈从那里接手。)

(In case anyone wants any background.. I came upon this while troubleshooting exceptions generated by NULL strings and .Length > x exceptions... in different places of the code. So when I saw the above code, in addition to everything else, my frustration took over from there.)

推荐答案

您'再以被称为短路一种语言特性的优势。这不是欺骗的语言,但是实际上使用特征很究竟如何设计为可以使用。

You're taking advantage of a language feature known as short circuiting. This is not cheating the language but in fact using a feature exactly how it was designed to be used.

这篇关于我不喜欢这...这是欺骗的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆