7

While implementing an == operator, I have the feeling that I am missing some essential points.
Hence, I am searching some best practices around that.
Here are some related questions I am thinking about:

  • How to cleanly handle the reference comparison?
  • Should it be implemented through a IEquatable<T>-like interface? Or overriding object.Equals?
  • And what about the != operator?

(this list might not be exhaustive).

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
remio
  • 1,242
  • 2
  • 15
  • 36
  • 3
    Check these threads: http://stackoverflow.com/questions/962960/equals-method-implementation-helpers-c, http://stackoverflow.com/questions/1421289/icomparable-and-equals, http://stackoverflow.com/questions/660566/override-equals-and-gethashcode-question, http://stackoverflow.com/questions/567642/how-to-best-implement-equals-for-custom-types – vgru Oct 06 '09 at 11:16

4 Answers4

10

I would follow Microsoft's Guidelines for Overloading Equals() and Operator ==.

edit: Microsoft's guidelines contain this important remark, which seems to confirm Henk's answer:

By default, the operator == tests for reference equality by determining if two references indicate the same object, so reference types do not need to implement operator == in order to gain this functionality. When a type is immutable, meaning the data contained in the instance cannot be changed, overloading operator == to compare value equality instead of reference equality can be useful because, as immutable objects, they can be considered the same as long as they have the same value. Overriding operator == in non-immutable types is not recommended

rianjs
  • 7,767
  • 5
  • 24
  • 40
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
  • Link is dead in 2018. (This is why we should quote relevant parts of the docs in addition to linking.) – rianjs Apr 02 '18 at 14:31
  • 1
    @rianjs isn't that exactly what I did? – Wim Coenen Apr 03 '18 at 13:05
  • "What are the best practices for implementing the `==` operator?" followed by "I would follow microsoft's guidelines " quoting a single line from what was once undoubtedly a whole page ("avoid doing it in mutable types") isn't a particularly good answer, no. It isn't complete; it's half a thought which isn't helpful to someone who wants to know the complete set of best practices around implementing a `==` operator. – rianjs Apr 03 '18 at 14:14
  • 2
    Quote expanded to the full paragraph it was taken from. – Wim Coenen Apr 04 '18 at 12:44
6

Each time you implement the == operator, be sure to also implement !=, IEquatable<T> and to override Object.Equals() and Object.GetHashCode() for consistency for the user of your class.

Considering a class, here's my usual implementation:

    public bool Equals(MyClass other) {
        if (ReferenceEquals(other, null))
            return false;
        if (ReferenceEquals(other, this))
            return true;
        return // your equality code here
    }

    public override bool Equals(object obj) {
        return Equals(obj as MyClass);
    }

    public override int GetHashCode() {
        return // your hash function here
    }

    public static bool operator ==(MyClass left, MyClass right) {
        return Equals(left, right);
    }

    public static bool operator !=(MyClass left, MyClass right) {
        return !(left == right);
    }
Julien Lebosquain
  • 40,639
  • 8
  • 105
  • 117
  • This. As well, if you implement a < (less than) operator initially, you can use that operator alone to implement every single other operator. ie: == can be expressed as !(left < right || right < left) if the < operator is already implemented. – Jesse O'Brien Oct 06 '09 at 12:33
4
  • If you implement ==, override .Equals and . GetHashCode
  • implement != as well
  • Check for null references using object.ReferenceEquals otherwise the operator will recurse
H H
  • 263,252
  • 30
  • 330
  • 514
dkackman
  • 15,179
  • 13
  • 69
  • 123
3

The most common approach is not to handle it. The default is reference comparison which in general is right for class objects.

So first you want to be very sure you need value-type behaviour.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 2
    Let there be clear answer of question rather then advice of telling someone to change the way they do things. – Akash Kava Oct 06 '09 at 11:22