System.IComparable Interface

Assembly: Mscorlib.dll
Namespace: System
Summary
Defines a generalized comparison method that a value type or class implements to create a type-specific comparison method.
C# Syntax:
public interface IComparable
Remarks
This interface is implemented by types whose values can be ordered; for example, the numeric and string classes.

A value type or class implements the IComparable.CompareTo method to create a type-specific comparison method suitable for purposes such as sorting.

See also:
System Namespace

System.IComparable Member List:

Public Methods
CompareTo Compares the current instance with another object of the same type.

System.IComparable Member Details

Method: CompareTo(
   object obj
)
Summary
Compares the current instance with another object of the same type.
C# Syntax:
int CompareTo(
   object obj
);
Parameters:

obj

An object to compare with this instance.

Return Value:
A 32-bit signed integer that indicates the relative order of the comparands. The return value has these meanings:

Value Meaning
Less than zero This instance is less than .
Zero This instance is equal to .
Greater than zero This instance is greater than .
Exceptions
Exception Type Condition
ArgumentException obj is not the same type as this instance.
Remarks
This method is only a definition and must be implemented by a specific class or value type to have effect. The meaning of the comparisons, "less than," "equal to," and "greater than," depends on the particular implementation.

By definition, any object compares greater than null; and two null references compare equal to each other.

The parameter, obj, must be the same type as the class or value type that implements this interface; otherwise, an ArgumentException is thrown.



Notes to implementors: For any objects A, B and C, the following must be true:

A.CompareTo(A) is required to return zero.

If A.CompareTo(B) returns zero then B.CompareTo(A) is required to return zero.

If A.CompareTo(B) returns zero and B.CompareTo(C) returns zero then A.CompareTo(C) is required to return zero.

If A.CompareTo(B) returns a value other than zero then B.CompareTo(A) is required to return a value of the opposite sign.

If A.CompareTo(B) returns a value x not equal to zero, and B.CompareTo(C) returns a value y of the same sign as x, then A.CompareTo(C) is required to return a value of the same sign as x and y.

Use the CompareTo method to determine the ordering of instances of a class.
See also:
Object

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.