IComparable(T) Interface
TOC
Collapse the table of content
Expand the table of content

IComparable<T> Interface

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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)

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

Type Parameters

in T

The type of objects to compare.

This type parameter is contravariant. That is, you can use either the type you specified or any type that is less derived. For more information about covariance and contravariance, see [2678dc63-c7f9-4590-9ddc-0a4df684d42e].

The IComparable<T> type exposes the following members.

  NameDescription
Public methodCompareToCompares the current object with another object of the same type.
Top

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 sorting methods such as the List<T>::Sort() method.

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.

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

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft