tags:

views:

2135

answers:

2

If i want to compare objects and they implement the IEquatable<> interface, i have a few questions:

  1. Why do i have to override Equals(object ) if i have to implements Equals<>
  2. can i use == and != once i implement IEquatable ?
+10  A: 

Regarding #1:

From MSDN:

If you implement IEquatable<T>, you should also override the base class implementations of Object::Equals(Object) and GetHashCode() so that their behavior is consistent with that of the IEquatable<T>::Equals method. If you do override Object::Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. This ensures that all invocations of the Equals() method return consistent results.

2) No, these do plain reference comparisons and do not use the Equals method.

Ray Booysen
so when you are dealing with objects, is == assumed to only mean the exact same memory address (same instance)
ooo
Pretty much. More info here:http://msdn.microsoft.com/en-us/library/53k8ybth(VS.80).aspx
Ray Booysen
No, use ReferenceEquals() for that purpose. The equality operator (==) usually means the same, but can be overriden (e.g. for Strings and the like).
Paul-Jan
Thanks Paul-Jan for clearing that up.
Ray Booysen
Slight terminology correction - you can't *override* operators, but you can *overload* them.
Jon Skeet
+6  A: 

1) As Ray said, override Equals(object) to ensure consistency when the method is called from classes which don't know (statically) that you implement IEquatable<T>. For instance, the non-generic collections classes will use Equals(object) for comparisons. You should also override GetHashCode().

2) Implementing IEquatable<T> doesn't overload the == and != operators automatically, but there's nothing to stop you from doing so, just like System.String does. You should document this very clearly if you do, however - and be careful when you make comparisons between other types of reference (e.g. MyType and Object) which will still use the identity comparison. I suspect it's not a great idea to do this unless it's going to be a very heavily used type in your code, where everyone will become very familiar with it and where the syntactic sugar of overloading == will really make a positive impact on readability.

Jon Skeet
Jon, is there any performance gain for an IEquatable<T> implementing type used for instance in a Collection<T>, calling the Contains method?
Rauhotz
It will avoid casting, yes. For value types, it'll avoid boxing and unboxing as well. See the docs for Collection<T>.Contains - it uses EqualityComparer<T>.Default, which will use the IEquatable<T> implementation if possible.
Jon Skeet
Yes, it is not a good idea to overload operator == and != to provide value equality checks (vs the default reference equality check). The MSDN documentation suggests you only do it for immutable types. There are also issues involving interfaces and operator overloading.
Burly
See http://stackoverflow.com/questions/728434/operator-overloading-with-interface-based-programming-in-c for more on both issues.
Burly