比较为null [英] Comparison to null
问题描述
你好,
为什么我有时会看到这个结构
//案例1
if(null == myVariable)
{
//...etc
}
// case 2
if(myVariable == null)
{
//...etc
}
基本上,这两者有什么区别?
Hello there,
Why is it that I sometimes see this construct
//case 1
if ( null == myVariable)
{
//...etc
}
//case 2
if ( myVariable == null)
{
//...etc
}
Basically, what is the difference between the two?
推荐答案
没什么区别, 他们是一样的。对于那些用C / C ++编程多年的人来说,语法很常见。
在C / C ++中,编写(单个'是合法的'='');
if(myVariable = null)
{
}
如果你切换它们会导致编译错误,因为你不能将
赋值为null。
if(null = myVariable)
{
}
克里斯
< ju **** **@hotmail.com>在消息中写道
news:11 ********************** @ l41g2000cwc.googlegr oups.com ...
It makes no difference, they are the same. The syntax is common for people
that have been programing in C/C++ for many years.
In C/C++ it is legal to write (single ''='');
if(myVariable = null)
{
}
If you switch them around, it causes a compilation error because you cannot
assign a value to null.
if(null = myVariable)
{
}
Chris
<ju******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
你好,
为什么我有时会看到这个结构
// case 1
if(null == myVariable)
{
//...etc
}
//案例2
if(myVariable == null)
{
/ /...etc
}
基本上,两者有什么区别?
Hello there,
Why is it that I sometimes see this construct
//case 1
if ( null == myVariable)
{
//...etc
}
//case 2
if ( myVariable == null)
{
//...etc
}
Basically, what is the difference between the two?
据我所知,没有区别。
我被教导使用if(null == myVariable)方法因为错误
喜欢使用a =而不是= =将在编译时捕获而不是
运行时。
-
消息通过 http://www.dotnetmonster.com
To my knowledge, there is no difference.
I was taught to use the if(null == myVariable) method because a mistake
like using a = instead of == will be caught at compile time instead of
runtime.
--
Message posted via http://www.dotnetmonster.com
Scott Klueppel通过DotNetMonster.com< fo *** @ DotNetMonster.com>写道:
Scott Klueppel via DotNetMonster.com <fo***@DotNetMonster.com> wrote:
据我所知,没有区别。
我被教导使用if(null == myVariable)方法因为一个错误
喜欢使用a =而不是==将在编译时捕获而不是运行时。
To my knowledge, there is no difference.
I was taught to use the if(null == myVariable) method because a mistake
like using a = instead of == will be caught at compile time instead of
runtime.
它们都将在编译时用C#捕获。在C#中使用这种不自然的订单没有
的理由。
-
Jon Skeet - < sk ***@pobox.com>
http://www.pobox.com /〜双向飞碟
如果回复小组,请不要给我发邮件
Both of them will be caught at compile-time with C# though. There''s no
reason to use this unnatural ordering in C#.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于比较为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!