This documentation is archived and is not being maintained.

IComparable(T) Interface

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

JScript does not support generic types or methods.

Type Parameters

T

The type of objects to compare.

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 CompareTo method to create a type-specific comparison method suitable for purposes such as sorting.

NoteNote:

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

The IComparable(T) 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(T).Sort() and Add.

Notes to Implementers:

Replace the type parameter of the IComparable(T) interface with the type that is implementing this interface.

The following code example illustrates the implementation of IComparable for a simple Temperature object. The example creates a SortedList(TKey, TValue) collection of strings with Temperature object keys, and adds several pairs of temperatures and strings to the list out of sequence. In the call to the Add method, the SortedList(TKey, TValue) collection uses the IComparable(T) implementation to sort the list entries, which are then displayed in order of increasing temperature.

No code example is currently available or this language may not be supported.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: