如果比较:价值第一 [英] if comparison: value first

查看:67
本文介绍了如果比较:价值第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些代码中看到,我现在不记得它是c#还是c ++,

当他们执行比较时他们先使用该值然后再使用

变量,如:


if(null == variable){}


有没有对此的解释或者是和往常一样

变量==价值?

-

Santi
www.syltek.com

I see in some code, I don′t remember now if it is c# or c++, that the
when they perform a comparison they use the value first and then the
variable, like:

if( null == variable ){}

Is there an explanation for this or is it the same as doing the usual
variable == value?
--
Santi
www.syltek.com

推荐答案

是否有解释或与通常的
变量==值相同?
Is there an explanation for this or is it the same as doing the usual
variable == value?




在C ++中人们使用它避免写错


如果(变量=值)


错误的话。这是完全有效的C ++代码(虽然现代编译器

生成警告),你可以在其中获得变量赋值而不是

a比较。

因为你不能在C#中犯这个错误,所以你写的东西无关紧要。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



In C++ people use it to avoid writing

if ( variable = value )

by mistake. That''s perfectly valid C++ code (though modern compilers
generate a warning) where you get an assignment to variable instead of
a comparison.

Since you can''t make that mistake in C#, it doesn''t matter which order
you write things in.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


嗨Santi,


它与做(变量==值)相同。我认为没有理由做其他事情比公司政策好。我当然发现阅读起来比常规变量==值更难,而且= / $
任何代码都很少找到值==变量。

-

快乐编码!

Morten Wennevik [C#MVP]
Hi Santi,

It is the same as doing (variable == value). No reason for doing it other
than company policy I suppose. I certainly find it harder to read than
the regular variable == value, and value == variable is rarely found in
any code.

--
Happy Coding!
Morten Wennevik [C# MVP]




Santi < SA **** @ nospam.es>在消息中写道

news:cr ********** @ nsnmrro2-gest.nuria.telefonica-data.net ...

"Santi" <sa****@nospam.es> wrote in message
news:cr**********@nsnmrro2-gest.nuria.telefonica-data.net...
我看到了一些代码,我现在不记得是否是c#或c ++,当他们执行比较时,他们首先使用值然后使用
变量,如:

if(null == variable){}

是否有解释或与通常的
变量==值相同?
I see in some code, I don′t remember now if it is c# or c++, that the
when they perform a comparison they use the value first and then the
variable, like:

if( null == variable ){}

Is there an explanation for this or is it the same as doing the usual
variable == value?




最终结果是一样的,但是有一个原因。

在C ++中,if语句对数字进行操作(0为假而非零)

是真的,大部分时间都是如此),因此有可能偶然发生


if(variable = 5)

{

}


并将if语句始终评估为true。执行5 =变量

会导致编译器抱怨您无法分配值,因此

捕获该错误并避免错误。


它在C#中不太重要,因为你只能用boolean

类型本身导致这个错误。



The end result is the same, however there is a reason.

In C++, if statements operated on numbers(0 being false and not-zero being
true, most of the time), therefore it was possible to accidentally do

if (variable = 5)
{
}

and have that if statement to always evaluate to true. Doing 5 = variable
causes the compiler to complain that you cannot assign to a value, thus
catching that error and avoiding a bug.

Its less important in C#, as you can only cause this error with boolean
types themselves.


这篇关于如果比较:价值第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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