Comparer<T> Class

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

Provides a base class for implementations of the IComparer<T> generic interface.

Inheritance Hierarchy

System.Object
  System.Collections.Generic.Comparer<T>

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

Syntax

'Declaration
Public MustInherit Class Comparer(Of T) _
    Implements IComparer, IComparer(Of T)
public abstract class Comparer<T> : IComparer, 
    IComparer<T>

Type Parameters

  • T
    The type of objects to compare.

The Comparer<T> type exposes the following members.

Constructors

  Name Description
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Comparer<T> Initializes a new instance of the Comparer<T> class.

Top

Properties

  Name Description
Public propertyStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Default Returns a default sort order comparer for the type specified by the generic argument.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Compare When overridden in a derived class, performs a comparison of two objects of the same type and returns a value indicating whether one object is less than, equal to, or greater than the other.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Explicit Interface Implementations

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

Top

Remarks

Derive from this class to provide a custom implementation of the IComparer<T> interface for use with collection classes.

The object returned by the Default property uses the System.IComparable<T> generic interface (IComparable<T> in C#, IComparable(Of T) in Visual Basic) to compare two objects. If type T does not implement the System.IComparable<T> generic interface, the Default property returns a Comparer<T> that uses the System.IComparable interface.

Notes to Implementers

Compare and EqualityComparer<T>.Equals may behave differently in terms of culture-sensitivity and case-sensitivity.

For string comparisons, the StringComparer class is recommended over Comparer<String>. Properties of the StringComparer class return predefined instances that perform string comparisons with different combinations of culture-sensitivity and case-sensitivity. The case-sensitivity and culture-sensitivity are consistent among the members of the same StringComparer instance.

For more information on culture-specific comparisons, see the System.Globalization namespace.

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.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.