Export (0) Print
Expand All

IEquatable(T).Equals Method

Indicates whether the current object is equal to another object of the same type.

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

abstract Equals : 
        other:'T -> bool

Parameters

other
Type: T

An object to compare with this object.

Return Value

Type: System.Boolean
true if the current object is equal to the other parameter; otherwise, false.

The implementation of the Equals method is intended to perform a test for equality with another object of type T, the same type as the current object. The Equals method is called in the following circumstances:

In other words, to handle the possibility that objects of a class will be stored in an array or a generic collection object, it is a good idea to implement IEquatable(T) so that the object can be easily identified and manipulated.

When implementing the Equals method, define equality appropriately for the type specified by the generic type argument. For example, if the type argument is Int32, define equality appropriately for the comparison of two 32-bit signed integers.

Notes to Implementers

If you implement Equals, you should also override the base class implementations of Object.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable(T).Equals method. If you do override Object.Equals(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, which the example illustrates.

The following example shows the partial implementation of a Person class that implements IEquatable(T) and has two properties, LastName and SSN. The Equals method returns True if the SSN property of two Person objects is identical; otherwise, it returns False.

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

Person objects can then be stored in a List(T) object and can be identified by the Contains method, as the following example shows.

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

.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
Show:
© 2015 Microsoft