检查空,哪个更好? "空=="或QUOT; == NULL"QUOT、NULL

2023-09-03 06:47:10 作者:没对象省流量

杜佩: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.