Otiel
Otiel

Reputation: 18743

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).
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?

Upvotes: 1

Views: 161

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500835

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.

Upvotes: 4

Related Questions