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

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

问题描述

重复:零差异

Dupe: 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) {}

文章解释说,后者是首选,因为它可以导致更准确的测试.从那以后,我一直在编码.现在,我了解的更多了,我正在寻找这篇文章或其他类似的文章,以了解确切的发现,但是我找不到关于该主题的任何信息.

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.

推荐答案

这是防止您键入 if(o = null)的老习惯.if(null = o)是语法错误.在C#中毫无意义,因为从来没有将null值强制转换为布尔值.

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.

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

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