When implemented by a class, compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
[Visual Basic] Function Compare( _ ByVal x As Object, _ ByVal y As Object _ ) As Integer [C#] int Compare( object x, object y ); [C++] int Compare( Object* x, Object* y ) = 0; [JScript] function Compare( x : Object, y : Object ) : int;
Value | Condition |
---|---|
Less than zero | x is less than y. |
Zero | x equals y. |
Greater than zero | x is greater than y. |
Exception Type | Condition |
---|---|
ArgumentException | Neither x nor y implements the IComparable interface.
-or- x and y are of different types. |
The preferred implementation is to use the CompareTo method of one of the parameters. If neither parameter implements IComparable, an exception must be thrown.
An exception must also be thrown if x and y are objects of different types and neither one can handle comparisons with the other.
a null reference (in Visual Basic Nothing) can always be compared with any other type; therefore, comparisons with a null reference (Nothing) must not generate an exception when using IComparable. When sorting, a null reference (Nothing) is considered to be less than any other object.
IComparer Interface | IComparer Members | System.Collections Namespace | IComparable