简体   繁体   English

实现IEqualityComparer会修改相等的测试结果吗?

[英]Implementing IEqualityComparer modifies equality test results?

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). 我为MyObject实现了IEqualityComparer<MyObject> ,以便我的优先级队列能够对元素进行排序(这里的用法并不真正导入,但无论如何)。
Thus, I implemented the Equals and GetHashCode methods. 因此,我实现了EqualsGetHashCode方法。

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? 我的问题是:当我做MyObject1 == MyObject2 ,它是否使用我在Equals方法中编写的测试,还是经典的相等测试?

Assuming this is a reference type, == will only perform any custom operations if you overload the == operator: 假设这是一个引用类型, ==只会在重载==运算符时执行任何自定义操作:

public static bool operator ==(MyClass1 x, MyClass1 y)
{
    ...
}

public static bool operator !=(MyClass1 x, MyClass1 y)
{
    ...
}

The C# compiler doesn't know about any relationship between the Equals method and the == operator, as far as I'm aware. 据我所知,C#编译器不知道Equals方法和==运算符之间的任何关系。

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

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