Class that implements IComparable interface comes with only one member, CompareTo method. CompareTo method compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

In short we can compare two elements of the same collection and as a result we get int value that tells us relative order of the objects being compared.

We can also compare two instances of the same class and result tells us weather results are the same, first value is greater or less than compared value.

Example using non-generic IComparable interface:

Example using generic IComparable interface:

Difference between these two approaches:

  1. Using non-generic interface IComparable requires of us to cast obj to desired type.
  2. Generic interface IComparable<T> is type safe.