This documentation is archived and is not being maintained.

EqualityComparer(T) Class

Updated: January 2010

Provides a base class for implementations of the IEqualityComparer(T) generic interface.

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

JScript does not support generic types or methods.

Type Parameters

T

The type of objects to compare.

Derive from this class to provide a custom implementation of the IEqualityComparer(T) generic interface for use with collection classes such as the Dictionary(TKey, TValue) generic class, or with methods such as List(T).Sort.

The Default property checks whether type T implements the System.IEquatable(T) generic interface and, if so, returns an EqualityComparer(T) that contains the implantation of the IEquitableEquals method. Otherwise, it returns an EqualityComparer(T), as provided by T.

We recommend that you derive from the EqualityComparer(T) class instead of implementing the IEqualityComparer(T) interface, because the EqualityComparer(T) class tests for equality using the IEquatable(T).Equals method instead of the Object.Equals method. This is consistent with the Contains, IndexOf, LastIndexOf, and Remove methods of the Dictionary(TKey, TValue) class and other generic collections.

The following example creates a dictionary collection of objects of type Box with an equality comparer. Two boxes are considered equal if their dimensions are the same. It then adds the boxes to the collection.

The dictionary is recreated with an equality comparer that defines equality in a different way: Two boxes are considered equal if their volumes are the same.

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

System.Object
  System.Collections.Generic.EqualityComparer(T)

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

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

Date

History

Reason

January 2010

Updated example.

Information enhancement.

Show: