Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
As well as defining standard protocols for equality, C# and .NET define standard protocols for determining the order of one object relative to another. The basic protocols are:
The IComparable interfaces
(IComparable and IComparable<T>)
The > and < operators
The IComparable interfaces are
used by general-purpose sorting algorithms. In the following example, the
static Array.Sort method works because System.String implements the IComparable interfaces:
string[] colors = { "Green", "Red", "Blue" };
Array.Sort (colors);
foreach (string c in colors) Console.Write (c + " "); // Blue Green Red
The < and > operators are more
specialized, and they are intended mostly for numeric types. Because they
are statically resolved, they can translate to highly efficient bytecode,
suitable for computationally intensive algorithms.