Compartilhar via


Why IComparable does not extend IEquatable [Krzysztof Cwalina]

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.

Comments

  • Anonymous
    April 29, 2006
    Remember how I have talked in the past about the difference between two different purposes for collation...
  • Anonymous
    April 29, 2006
    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.