Export (0) Print
Expand All

Comparer<T> Class

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

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

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

[SerializableAttribute]
generic<typename T>
public ref class Comparer abstract : IComparer, 
	IComparer<T>

Type Parameters

T

The type of objects to compare.

The Comparer<T> type exposes the following members.

  NameDescription
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryComparer<T>Initializes a new instance of the Comparer<T> class.
Top

  NameDescription
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryDefaultReturns a default sort order comparer for the type specified by the generic argument.
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryCompareWhen 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 the XNA FrameworkSupported by Portable Class LibraryEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIComparer::CompareCompares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
Top

Derive from this class to provide a custom implementation of the IComparer<T> interface for use with collection classes such as the SortedList<TKey, TValue> and SortedDictionary<TKey, TValue> generic classes.

The difference between deriving from the Comparer<T> class and implementing the System::IComparable interface is as follows:

  • To specify how two objects should be compared by default, implement the System::IComparable interface in your class. This ensures that sort operations will use the default comparison code that you provided.

  • To define a comparer to use instead of the default comparer, derive from the Comparer<T> class. You can then use this comparer in sort operations that take a comparer as a parameter.

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 and Encoding and Localization.

The following example derives a class, BoxLengthFirst, from the Comparer<T> class. This comparer compares two objects of type Box. It sorts them first by length, then by height, and then by width. The Box class implements the IComparable<T> interface to control the default comparison between two Box objects. This default implementation sorts first by height, then by length, and then by width. The example shows the differences between the two comparisons by sorting a list of Box objects first by using the BoxLengthFirst comparer and then by using the default comparer.

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

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

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

Community Additions

ADD
Show:
© 2014 Microsoft