实施 IEqualityComparer 会修改相等测试结果吗?
我为 MyObject
实现了一个 IEqualityComparer
,以便我的优先级队列能够对元素进行排序(这里的使用并不真正导入,但无论如何)。
因此,我实现了 Equals
和 GetHashCode
方法。
我的问题是:当我执行 MyObject1 == MyObject2
时,它是使用我在 Equals
方法中编写的测试还是经典的相等测试?
I implemented a IEqualityComparer<MyObject>
for MyObject
in order for my priority queue to be able to sort elements (the use does not really import here, but whatever).
Thus, I implemented the Equals
and GetHashCode
methods.
My question is: when I do MyObject1 == MyObject2
, does it use the tests written by me in the Equals
method or is it a classical equality test?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设这是一个引用类型,如果您重载 == 运算符,
==
将仅执行任何自定义操作:C# 编译器不知道
Equals
之间的任何关系据我所知,方法和==
运算符。Assuming this is a reference type,
==
will only perform any custom operations if you overload the == operator:The C# compiler doesn't know about any relationship between the
Equals
method and the==
operator, as far as I'm aware.