Compares two objects of the same type and returns a value indicating whether one is less than, equal to, or greater than the other.
[Visual Basic] Overridable Public Function Compare( _ ByVal a As Object, _ ByVal b As Object _ ) As Integer [C#] public virtual int Compare( object a, object b ); [C++] public: virtual int Compare( Object* a, Object* b ); [JScript] public function Compare( a : Object, b : Object ) : int;
Value | Condition |
---|---|
Less than zero | a is less than b. |
Zero | a equals b. |
Greater than zero | a is greater than b. |
Exception Type | Condition |
---|---|
ArgumentException | Neither a nor b implements the IComparable interface.
-or- a and b are of different types. |
If a implements IComparable, then a.CompareTo(b) is returned; otherwise, if b implements IComparable, then b.CompareTo(a) is returned. If neither parameter implements IComparable, an exception is thrown.
An exception must also be thrown if a and b 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) will not generate an exception when using IComparable. When sorting, a null reference (Nothing) is considered to be less than any other object.
Comparer Class | Comparer Members | System.Collections Namespace | IComparable