How to declare natural ordering by implementing the generic IComparable interface in C# .NET

Primitive types such as integers can be ordered naturally in some way. Numeric and alphabetical ordering comes in handy with numbers and strings. However, there’s no natural ordering for your own custom objects with a number of properties.

Imagine that you want to be able to declare that one triangle is larger or smaller than another one in a natural way. One way to declare natural comparison is by implementing the generic IComparable interface which comes with one method, CompareTo, which returns an integer. The method should return 0 if the compared instances are equal. It should return an integer larger than 0 if “this” current instance is larger/higher/longer etc. than the instance it is compared to. The reverse is true if “this” instance is ranked behind the other instance.