检查空,哪个更好? "空=="或QUOT; == NULL" [英] Checking for null, which is better? "null ==" or "==null"

查看:138
本文介绍了检查空,哪个更好? "空=="或QUOT; == NULL"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

杜佩:<一href="http://stackoverflow.com/questions/302701/null-difference">http://stackoverflow.com/questions/302701/null-difference

一个终身前,我碰到了解释,以下是不相等的(在C#)的一篇​​文章:

A lifetime ago I came across an article that explained that the following were not equal (in c#):

if (o == null) {}
if (null == o) {}

文章解释,后者是preferred因为它导致更准确的测试。我一直在编码一样,自从。现在我明白了这么多我一直在寻找的物品,或其他喜欢它,来看看具体的调查结果,但是我无法找到关于这个问题的任何东西。

The article explained that the latter was preferred because it resulted in a more accurate test. I've been coding like that ever since. Now that I understand so much more I was looking for the article, or another like it, to see what the exact findings were, but I can't find anything on the subject.

思考?有区别吗?乍一看会说不。但谁知道在IL和C#编写的肠子会发生什么。

Thoughts? Is there a difference? First glance would say no. But who knows what happens in the bowels of IL and C# compilation.

推荐答案

这是一个老习惯,以prevent你键入如果(O = NULL)。如果(空= O)是一个语法错误。一种毫无意义在C#中,因为空值,所以以往任何时候都被迫布尔值。

it's an old habit to prevent you from typing if (o = null). if (null = o) is a syntax error. kind of pointless in C#, because null values aren't ever coerced into booleans.

这篇关于检查空,哪个更好? &QUOT;空==&QUOT;或QUOT; == NULL&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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