在一个字面值相等比较不会操作数的顺序很重要? [英] In equal comparison of a literal value does the order of operands matter?

查看:119
本文介绍了在一个字面值相等比较不会操作数的顺序很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯写code为(只是一个例子)

I am accustomed to write the code as(just an example)

Request.QueryString["xxxx"] != null

最近有人说,

Recently someone said that

null != Request.QueryString["xxxx"]

提供了更好的性能。

gives a better performance.

我很好奇,想知道它是否真正带来任何区别或没有,如果是的话怎么办?

I am curious to know as whether it actually brings any difference or not and if so how?

请注意〜以上只是一个例子。待一般来讲

Note~ The above is just an example. To be generically speaking

无论

Constant [Operator] Actual Value (e.g. 1 == Convert.ToInt32(textbox1.text))

Actual Value [Operator] Constant (e.g. Convert.ToInt32(textbox1.text) == 1)

感谢

推荐答案

不,这不是一般的事实。比较运营商需要同时评估其两侧所以在把​​常量左侧没有收获。但是,让他们在左边(叫尤达风格)减少编码的语言错误,你被允许使用赋值运算符的内部条件和您无意中键入的比较操作符 == 作为一个单一的 =

No, it's not true in general. Comparison operators need to evaluate both their sides so there is no gain in putting constants on the left. But having them on the left hand side (called Yoda style) reduces coding errors in languages where you are allowed to use assignment operator inside a conditional and you unintentionally mistyped the comparison operator == as a single =:

// What you intended to write
if (a == 6) ...
// What you wrote instead
if (a = 6) ... // --> always true as the value of a is changed to 6
// What if Yoda style is used
if (6 = a) ... // --> compile time error

这篇关于在一个字面值相等比较不会操作数的顺序很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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