Tuesday, March 29, 2011

Equals vs IEquatable

IEquatable is not a replacement of Object.Equals. If your object implements IEquatable<T> it still HAS TO override Equals and GetHashCode http://blogs.msdn.com/b/jaredpar/archive/2009/01/15/if-you-implement-iequatable-t-you-still-must-override-object-s-equals-and-gethashcode.aspx 


Here is how the MSDN docs puts it:
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. http://msdn.microsoft.com/en-us/library/ms131187.aspx