What your class is comparable to is up to you. You could make it comparable to anything, as the non-generic version of the IComparable
interface takes an object
; in the implementation of CompareTo
method, you can basically sniff the type of the object (unless it is null
).
When you use the generic version of IComparable<T>
, you're making the statement that you implement a strongly-typed comparison method to whatever the type parameter T
is.
Generally, if you have an IComparable<T>
implementation for one type, you probably should have an IEquatable<T>
implementation as well.
Having an IComparable/IComparable<T>
implementation means that what is being represented by the structure has a natural ordering to it. Ordering also implies that there can be equality between two instances: if instance a
of T
is not greater than or less than instance b
of T
then it must be equal.
To that end, if you implement IComparable/IComparable<T>
, then logic dictates that you implement IEquatable/IEquatable<T>
.
Of course, when you implement IEquatable/IEquatable<T>
, you should override Equals
and GetHashCode
.
Which in turn means that you should override all the comparison operators:
==
(which implies!=
)<
and<=
>
and>=