System.IComparable<T>

Defines a generalized comparison method that a value type or class implements to create a type-specific comparison method for ordering instances.

See Also: IComparable<T> Members

Syntax

public interface IComparable<T>

Type Parameters

T
Documentation for this section has not yet been entered.

Remarks

This interface is implemented by types whose values can be ordered, primarily for sorting. For example, one number can be larger than a second number, and one string can appear in alphabetical order before another. A value type or class implements the IComparable`1.CompareTo(`0) method to create a type-specific comparison method suitable for purposes such as sorting.

The IComparable`1 interface defines the IComparable`1.CompareTo(`0) method, which determines the sort order of instances of the implementing type. The IEquatable`1 interface defines the IEquatable`1.Equals(`0) method, which determines the equality of instances of the implementing type.

The implementation of the IComparable`1.CompareTo(`0) method must return an int that has one of three values, as shown in the following table.

Less than zero

This object is less than the object specified by the IComparable`1.CompareTo(`0) method.

Zero

This object is equal to the method parameter.

Greater than zero

This object is greater than the method parameter.

The IComparable`1 interface provides a strongly typed comparison method for ordering members of a generic collection object. Because of this, it is usually not called directly from developer code. Instead, it is called automatically by methods such as List`1.Sort and SortedList`2.Add(`0, `1).

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0