views:

233

answers:

5

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

+9  A: 

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:

Overriding operator == in non-immutable types is not recommended.

Wim Coenen
+3  A: 
  • If you implement ==, override .Equals and . GetHashCode
  • implement != as well
  • Check for null references using object.ReferenceEquals otherwise the operator will recurse
dkackman
Actually, you HAVE to implement != as well, otherwise C# won't let you compile.
Maximilian Mayerl
Good point. Forgot that.
dkackman
+2  A: 

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
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
+5  A: 

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.

Henk Holterman
Let there be clear answer of question rather then advice of telling someone to change the way they do things.
Akash Kava
A: 

Use Resharper for such tasks. There is no point in reinventing the wheel.

Pawel Lesnikowski
That's an expensive solution for such a trivial task
spender
Further, for some tasks, such as this, I'd much rather that a developer *learned how to do something properly* than to have some tool do the work for him, and never thought about it again.
Mike Hofer
@spender I'm sure you know that R# can be used not only for such trivial tasks. @Mike They should be able to write this by hand, but they they should also have tools to be more productive and not have to write it.
Pawel Lesnikowski