Why IComparable<T> does not extend IEquatable<T> [Krzysztof Cwalina]

Why IComparable<T> does not extend IEquatable<T> [Krzysztof Cwalina]

  • Comments 2

Recently, somebody asked me why IComparable<T> does not extend IEquatable<T>. Here is this piece of history.

 

When we were designing IComparer<T> and IEqualityComparer<T>, we observed that these two interfaces are rarely (never?) consumed together. Collections that sort consume IComparer<T>; collections that don’t sort, consume IEqualityComparer<T>. If IComparer<T> extended IEqualityComparer<T>, users who want to implement a custom comparer to be used in a sorting collection would have to implement both interfaces, even though they would typically case less what the Equal method does.

 

Because of that we decided to leave IEqualityComparer<T> and IComparer<T> independent and we did the same thing with IComparable<T> and IEquatable<T> for consistency.

  • Remember how I have talked in the past about the difference between two different purposes for collation...
  • You give very complex answer.

    Here is simpler one - if you are asked to order somebody based on their age - does it mean that folks with same age will be same?
    I.e. ordering items does not mean that you can figure out if some items are equal.
    Especially taking in account that not all sort algorithms are stable and there can be different sort results based on initial ordering.
Page 1 of 1 (2 items)