最好和最快的方法来检查对象是否为空

最好和最快的方法来检查对象是否为空

问题描述:

我经常在源$ 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.