ReferenceEquals(variable, null) 和 variable == null 一样吗? [英] ReferenceEquals(variable, null) is the same as variable == null?

查看:40
本文介绍了ReferenceEquals(variable, null) 和 variable == null 一样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上是标题.我在我正在处理的代码中看到了很多前者,我想知道他们为什么不使用后者.两者有区别吗?

Basically the title. I see a lot of the former in the code I'm working on and I was wondering why they didn't use the latter. Are there any differences between the two?

谢谢.

推荐答案

直接来自 文档

与 Equals 方法和相等运算符不同,ReferenceEquals 方法不能被覆盖.因此,如果要测试两个对象引用的相等性并且不确定 Equals 方法的实现,则可以调用 ReferenceEquals 方法.但是请注意,如果 objA 和 objB 是值类型,则它们在传递给 ReferenceEquals 方法之前会被装箱.

Unlike the Equals method and the equality operator, the ReferenceEquals method cannot be overridden. Because of this, if you want to test two object references for equality and are unsure about the implementation of the Equals method, you can call the ReferenceEquals method. However, note that if objA and objB are value types, they are boxed before they are passed to the ReferenceEquals method.

这篇关于ReferenceEquals(variable, null) 和 variable == null 一样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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