Export (0) Print
Expand All

EqualityComparer(T) Class

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

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

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

[<AbstractClass>]
[<SerializableAttribute>]
type EqualityComparer<'T> =  
    class
        interface IEqualityComparer
        interface IEqualityComparer<'T>
    end

Type Parameters

T

The type of objects to compare.

The EqualityComparer(T) type exposes the following members.

  NameDescription
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryEqualityComparer(T)Initializes a new instance of the EqualityComparer(T) class.
Top

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

  NameDescription
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.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(T, T)When overridden in a derived class, determines whether two objects of type T are equal.
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 LibraryGetHashCode()Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCode(T)When overridden in a derived class, serves as a hash function for the specified object for hashing algorithms and data structures, such as a hash table.
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 FrameworkIEqualityComparer.EqualsDetermines whether the specified objects are equal.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIEqualityComparer.GetHashCodeReturns a hash code for the specified object.
Top

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 IEquatable(T).Equals 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.

.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