简体   繁体   English

为什么Object.Equals(new Object(),new Object())返回false

[英]Why does Object.Equals(new Object(), new Object()) return false

Why does the following piece of code print false? 为什么以下代码显示为假?

static void Main(string[] args)
{
    Console.WriteLine(Object.Equals(new Object(), new Object()));
    Console.ReadKey();
}

From MSDN MSDN

The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Equals的默认实现支持引用类型的引用相等性和值类型的按位相等性。 Reference equality means the object references that are compared refer to the same object. 引用相等意味着比较的对象引用引用同一对象。 Bitwise equality means the objects that are compared have the same binary representation. 按位相等意味着所比较的对象具有相同的二进制表示形式。

In other words, you are creating two different objects of the same type and seeing if they are the exact same object, which they are not. 换句话说,您正在创建两个相同类型的不同对象,并查看它们是否是完全相同的对象,而不是。

According to the MSDN Documentation : 根据MSDN文档

The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Equals的默认实现支持引用类型的引用相等性和值类型的按位相等性。 Reference equality means the object references that are compared refer to the same object . 引用相等意味着比较的对象引用引用相同的对象 Bitwise equality means the objects that are compared have the same binary representation. 按位相等意味着所比较的对象具有相同的二进制表示形式。

Note that a derived type might override the Equals method to implement value equality. 请注意,派生类型可能会覆盖Equals方法以实现值相等。 Value equality means the compared objects have the same value but different binary representations. 值相等意味着被比较的对象具有相同的值,但具有不同的二进制表示形式。

You are creating two different Objects. 您正在创建两个不同的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM