C# 中 obj1.Equals(obj2) 和 static Object.Equals(obj1, obj2) 有什么区别?

发布于 2024-09-19 16:01:59 字数 277 浏览 10 评论 0原文

从微软的文档来看,两种 Equals 方法本质上是相同的。但我偶然发现了一些非常奇怪的事情。 在我的 Silverlight 项目中,我有两个覆盖 Equals 的同一类实例。 如果我要求 inst1.Equals(inst2)inst2.Equals(inst1) 我总是得到 true 结果。但Object.Equals(inst1, inst2)返回false。这怎么可能?

有什么想法吗?

谢谢, 洛科

from the documentation by Microsoft, both Equals-methods are essentially the same. But I just stumbled across something very strange.
in my Silverlight project I have two instances of the same class that overrides Equals.
If I ask for inst1.Equals(inst2) or inst2.Equals(inst1) I always get true as result. But Object.Equals(inst1, inst2) returns false. How is this possible?

Any Ideas?

Thanks,
Rocko

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

め七分饶幸 2024-09-26 16:01:59

obj1.Equals 假定 obj1 不为 nullobject.Equals 甚至适用于 null 值。但这并不能解释你所看到的行为;我认为您应该提供一些代码来重现它以获得更好的答案。

obj1.Equals assumes that obj1 is not null. object.Equals works even on null values. That doesn't explain the behavior you see though; I think you should provide some code to reproduce it for a better answer.

爱殇璃 2024-09-26 16:01:59

obj1.Equals 可以被覆盖,Object.Equals 不能。换句话说,Object.Equals 是 Equals 方法的基本实现,如果您不重写它,则可以免费获得它。由于您确实覆盖了它,因此这两种实现是不同的,并且可能会产生不同的结果。

obj1.Equals can be overridden, Object.Equals cannot. In other words Object.Equals is the base implementation of the Equals method that you get for free if you don't override it. Since you did override it, the two implementations are different and may produce different results.

假面具 2024-09-26 16:01:59

我认为 Object.Equals 将测试两个参数是否是相同的引用,即它们指向相同的内存空间。

MyClass.Equals 可能有不同的实现,这样两个不同引用的类实际上可能是相等的(基于它们的字段和属性)。

I think that Object.Equals will test if the 2 arguments are the same reference, that is, they point to the same memory space.

MyClass.Equals may have a different implementation, such that 2 classes that aren't the same reference may in fact be equal (based on their fields and properties).

屋檐 2024-09-26 16:01:59

请小心正确实现 IEquatable。我犯了以下错误:

public class SubjectDTO: IEquatable<SubjectDTO>
{
    public string Id;

    public bool Equals(SubjectDTO other)
    {
        return Object.Equals(Id, other.Id);
    }

    public override int GetHashCode()
    {
        return Id == null ? 1 : Id.GetHashCode();
    }
}

看起来不错,对吧?但当你尝试时,你会发现令人惊讶的结果:

var a = new SubjectDTO() { Id = "1"};
var b = new SubjectDTO() { Id = "1"};
Console.WriteLine(Object.Equals(a, b));
Console.WriteLine(a.Equals(b));

False
True

啊?嗯,覆盖 Equals(object other) 非常重要:

public override bool Equals(object other)
{
    return other == null ? false : Equals(other as SubjectDTO);
}

当您将其添加到 SubjectDTO 类时,它将按预期工作。

Be careful to properly implement IEquatable<T>. I did the following mistake:

public class SubjectDTO: IEquatable<SubjectDTO>
{
    public string Id;

    public bool Equals(SubjectDTO other)
    {
        return Object.Equals(Id, other.Id);
    }

    public override int GetHashCode()
    {
        return Id == null ? 1 : Id.GetHashCode();
    }
}

Looks ok, right? But when you try it, you find surprising result:

var a = new SubjectDTO() { Id = "1"};
var b = new SubjectDTO() { Id = "1"};
Console.WriteLine(Object.Equals(a, b));
Console.WriteLine(a.Equals(b));

False
True

Huh? Well, it is important to overide Equals(object other):

public override bool Equals(object other)
{
    return other == null ? false : Equals(other as SubjectDTO);
}

When you add it to the SubjectDTO class, it will work as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文