最好和最快的方法来检查对象是否为空 [英] Best and fastest way to check if an object is null

查看:113
本文介绍了最好和最快的方法来检查对象是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在源$ C ​​$ CS的如果(object.ReferenceEquals(myObject的,空))作为检查的用法是否myObject的是空的,而不是如果(myObject的== NULL )这是我很熟悉。

I often see in source codes the usage of if (object.ReferenceEquals(myObject, null)) for checking if myObject is null instead of if (myObject == null) which I am familiar with.

是否有使用第一方式,而不是第二个任何特别的原因(如速度,可读性等)?你用哪一个?

Is there any particular reason (like speed, readability, etc) for using the first way instead of the second one? Which one do you use?

感谢你在前进。

推荐答案

在使用的ReferenceEquals 您确保任何特殊处理(重载运算符的实例)被应用。这也导致了不同的结果,如果使用未结合泛型

When using ReferenceEquals you make sure that no special handling (overloaded operator for instance) is being applied. This also leads to different results if used with unbound generics.

这篇关于最好和最快的方法来检查对象是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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