比较两个对象
使用 Object.Equals 会导致两个使用相同构造函数新创建的对象返回 false。这是为什么呢?我希望能够比较两个对象并查看它们是否相同,换句话说,我想做与上面相同的测试,但我希望能够在第一次测试中得到 true (因为这两个对象应该是相同的)。
如何在不为任何特定类构建自己的比较方法的情况下实现这一目标。
using System;
class Program
{
static void Main(string[] args)
{
Object Obj1 = new Object();
Object Obj2 = new Object();
Console.WriteLine(Obj1.Equals(Obj2));
Obj2 = Obj1;
Console.WriteLine(Obj1.Equals(Obj2));
}
}
/* This example produces the following output:
False
True
*/
附录:
Object o1 = new Object();
Object o2 = new Object();
o1.value1 = 1;
o2.value1 = 1;
//Now I want to compare o1 and o2 and see if they are equal
//Can I do this without building my own function?
Using Object.Equals results in a false with two newly created objects using the same constructor. Why is this? I want to be able to compare two objects and see if they are the same, in other words, I want to do the same test as above, but I want to be able to get a true on the first test (since the two objects should be identical).
How can I accomplish this without building my own compare method for any particular class.
using System;
class Program
{
static void Main(string[] args)
{
Object Obj1 = new Object();
Object Obj2 = new Object();
Console.WriteLine(Obj1.Equals(Obj2));
Obj2 = Obj1;
Console.WriteLine(Obj1.Equals(Obj2));
}
}
/* This example produces the following output:
False
True
*/
Addendum:
Object o1 = new Object();
Object o2 = new Object();
o1.value1 = 1;
o2.value1 = 1;
//Now I want to compare o1 and o2 and see if they are equal
//Can I do this without building my own function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Object.Equals()
正在使用引用比较的相等性,只有当它是相同的对象引用时才会结果为 true。您可以为自己的从 Object 派生的类型重写此行为。来自 MSDN:
Object.Equals()
is using reference equality for comparing, which will only result in true if it's the same object reference. You can override this behavior for your own types that derive from Object.From MSDN:
当我做出这个答案时我很着急,这就是我重写我的答案的原因。
当您使用相同的数据创建同一个类两次时,equals 方法会检查两个对象的引用是否相等,它们仍然存储在内存中的不同位置,因此它们的引用不相等,并且 Equals 方法返回 false。
要检查类的值是否相等,您必须重写 Equals 方法,同时要使用 == 和 != 运算符,我们需要重载它们。
例如,字符串类会覆盖它,如果它不这样做,则会打印 false:
示例:
This is Foo in the first example:
This is Foo in the second example:
注意: 当覆盖 Equals 时或重载 ==/!=,Visual Studio 希望您重写 GetHashCode 函数。该函数对于 Foo 的每个实例都应该是唯一的,并且具有相同的值。
I was in a hurry when I made this answer, that's why I've rewritten my answer.
The equals method checks if references of two objects equal, when you create the same class twice with the same data, they're are still stored at different positions in memory, therefore their reference doesn't equal, and the Equals method returns false.
To check whenever the value of the classes equal you'll have to override the Equals method, also to make use of the == and != operators we'll need to overload those.
For example the string class overrides it, if it wouldn't do that this would print false:
Examples:
This is Foo in the first example:
This is Foo in the second example:
NOTE: When overriding Equals or overloading ==/!=, Visual Studio want you to override the GetHashCode function. That function should be unique for every instance of Foo, with the same values.
大多数答案都是正确的,我只会提供更多细节:
从 System.Object 派生的类的任何实例(除了 System.ValueType 类之外)都是“引用”类。 “参考”类有两部分:实例的“核心”被放置在程序的内存中,称为“堆”,然后指向该内存地址的“引用”或“指针”被放置在堆栈中。有关堆栈和堆是什么以及为什么需要它们的更多信息,请参阅 MSDN 文档或计算机编程基本入门书。
在.NET中,引用类型的两个变量的相等性默认比较是比较变量存储的内存地址。这就是“引用相等”;如果两个变量指向相同的内存地址,则它们相等。如果没有,他们就不是。
对于“语义”或“结构”平等,需要更深入地了解每个变量的内存空间中的内容。一般情况下不能这样做;开发人员必须为他想要比较的每个类定义什么使他的类的两个实例在语义上相等。这是通过重写从 Object 类继承的 Equals() 方法来实现的(因此对所有 .NET 类都是通用的)。 Equals 重写通常具有以下结构:
实现 IComparable、IEquatable 和 IStructuralEquatable 接口向使用者提供当前类的作者具有相等或其他比较的自定义定义的线索。它们在某些情况下是必要的,但并非在所有情况下都是必要的。
Most of the answers are correct, I'll just give a little more detail:
Any instance of a class deriving from System.Object, except for those that are System.ValueType classes, is a "reference" class. A "reference" class has two parts; the "meat" of the instance is placed in the program's memory, called the "heap", and then a "reference" or "pointer" to that memory address is placed in the stack. For more information on what the stack and heap are and why they're necessary, consult the MSDN docs or a basic primer in computer programming.
In .NET, the default comparison of equality of two variables of a reference type is to compare the memory addresses stored by the variables. This is "reference equality"; if two variables point to the same memory address, they are equal. If not, they're not.
For "semantic" or "structural" equality, a deeper look at what lives in each variable's memory space is required. This can't be done in the general case; the developer must define, for each class he wishes to compare, what makes two instances of his class semantically equal. This is accomplished by overriding the Equals() method which is inherited from the Object class (and thus common to all .NET classes). An Equals override generally has the following structure:
Implementing the IComparable, IEquatable and IStructuralEquatable interfaces provide clues to consumers that the author of the current class has a custom definition of equality or other comparison. They are necessary in some cases, not in all.
我相信你必须创建自己的比较方法,并实现 ICompare 接口
I believe you have to create your own compare method, and implement the ICompare interface
Object.Equals() 是引用相等。您必须构建自己的比较方法,无论是重写 equals,还是实现 IEqualityComparer,或任何其他比较接口。
Object.Equals() is reference equality. You must build your own comparison method, be it overriding equals, or implementing IEqualityComparer, or any of the other comparison interfaces.
与按值比较的基元不同,使用对象的引用来比较对象。这经常困扰字符串新手,在很多语言中字符串都是对象,他们只是尝试使用原始比较来检查它们的相等性。
如果您确实希望能够使用
==
运算符进行比较,那么您可以 重载运算符。Objects are compared using the reference to the object, unlike primitives that are compared by value. This frequently plagues newbies with strings, which in a lot of languages are Objects and they simply try to check their equality using the primitive comparison.
If you really want to be able to compare using the
==
operator, then you can overload the operator.除了引用标识之外,
object
类型绝对没有任何区别特征。如果您希望两个不同的object
实例比较相等,只需实现您自己的比较器:哦等等,这就是您不想要做的。好吧,然后创建您自己的类型,使其按照您想要的方式运行:
您不能只更改
object
类型本身。The
object
type has absolutely no distinguishing characteristics other than reference identity. If you want two distinctobject
instances to compare equal, justimplement your own comparer:Oh wait, that's what you don't want to do. Well, then make your own type that behaves the way you want:
You can't just change the
object
type itself.对象类的 equals 方法比较两个引用。由于您正在创建一个新对象,因此它们永远不会相等。
The equals method for the object class compares the two references. Since you are creating a new object they will never be equal.