In most places I read that it is a good idea to inherit from both IComparable
and IComparable<T>
in your classes to provide compatibility with non-generic collections. My question is why IComparable<T>
doesn't inherit from IComparable
by default then? The backward compatibility would be achieved this way.
For example, IEnumerable<T>
does inherit from IEnumerable
, so why don't do the same thing with IComparable
? I read about variances, but I still don't really understand why it was designed that way. I tried to create a small example with interfaces with the same architecture, and it works as intended.
Edit:
Also, Microsoft documentation states the following:
Generic interfaces can inherit from non-generic interfaces if the generic interface is contravariant, which means it only uses its type parameter as a return value. In the .NET Framework class library, IEnumerable<T>
inherits from IEnumerable
because IEnumerable<T>
only uses T in the return value of GetEnumerator
and in the Current
property getter.
But despite that, one can inherit IComparable
from IComparable<T>
just fine (if you create interfaces with same architectures, that is).