Compartilhar via


IComparer<T> Interface

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Defines a method that a type implements to compare two objects.

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

Syntax

'Declaration
Public Interface IComparer(Of In T)
public interface IComparer<in T>

Type Parameters

  • inT
    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 IComparer<T> type exposes the following members.

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Compare Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.

Top

Remarks

This interface is used with the List<T>.Sort and List<T>.BinarySearch methods. It provides a way to customize the sort order of a collection.

The default implementation of this interface is the Comparer<T> class. The StringComparer class implements this interface for type String.

This interface supports ordering comparisons. That is, when the Compare method returns 0, it means that two objects sort the same. Implementation of exact equality comparisons is provided by the IEqualityComparer<T> generic interface.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.