.NET Framework Class Library
IEquatableT Interface

Defines a generalized method that a value type or class implements to create a type-specific method for determining equality of instances.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Interface IEquatable(Of T)
public interface IEquatable<T>
generic<typename T>
public interface class IEquatable
type IEquatable<'T> =  interface end
JScript does not support generic types or methods.

Type Parameters

T

The type of objects to compare.

The IEquatableT type exposes the following members.

Methods
  NameDescription
Public method Supported by the XNA Framework Supported by Portable Class Library EqualsIndicates whether the current object is equal to another object of the same type.
Top
Remarks

This interface is implemented by types whose values can be equated (for example, the numeric and string classes). A value type or class implements the Equals method to create a type-specific method suitable for determining equality of instances.

NoteNote

The IComparableT interface defines the CompareTo method, which determines the sort order of instances of the implementing type. The IEquatableT interface defines the Equals method, which determines the equality of instances of the implementing type.

The IEquatableT interface is used by generic collection objects such as DictionaryTKey, TValue, ListT, and LinkedListT when testing for equality in such methods as Contains, IndexOf, LastIndexOf, and Remove. It should be implemented for any object that might be stored in a generic collection.

Notes to Implementers

Replace the type parameter of the IEquatableT interface with the type that is implementing this interface.

If you implement IEquatableT, you should also override the base class implementations of ObjectEquals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatableTEquals method. If you do override ObjectEquals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. In addition, you should overload the op_Equality and op_Inequality operators. This ensures that all tests for equality return consistent results.

NoteNote

For information on overriding Equals(Object), see the ObjectEquals(Object) article.

For a value type, you should always implement IEquatableT and override ObjectEquals(Object) for better performance. ObjectEquals boxes value types and relies on reflection to compare two values for equality. Both your implementation of Equals and your override of ObjectEquals should return consistent results.

If you implement IEquatableT, you should also implement IComparableT if instances of your type can be ordered or sorted. If your type implements IComparableT, you should also always implement IEquatableT.

Examples

See the example for the IEquatableTEquals method.

Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8