IEquatable Interface
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Defines a generalized method that a value type or class implements to create a type-specific method for determining equality of instances.
generic
public interface class IEquatable
public interface IEquatable
public interface IEquatable where T : allows ref struct
type IEquatable<'T> = interface
Public Interface IEquatable(Of T)
Type Parameters
- T
The type of objects to compare.
- Derived
Examples
The following example shows the partial implementation of a Person class that implements IEquatableLastName and NationalId. NationalId is considered to be a unique identifier, therefore the Equals method returns True if the NationalId property of two Person objects is identical; otherwise, it returns False.
(Note that the F# example does not handle null values for Person instances.)
public class Person : IEquatable
{
public Person(string lastName, string ssn)
{
LastName = lastName;
NationalId = ssn;
}
public string LastName { get; }
public string NationalId { get; }
public bool Equals(Person? other) => other is not null && other.NationalId == NationalId;
public override bool Equals(object? obj) => Equals(obj as Person);
public override int GetHashCode() => NationalId.GetHashCode();
public static bool operator ==(Person person1, Person person2)
{
if (person1 is null)
{
return person2 is null;
}
return person1.Equals(person2);
}
public static bool operator !=(Person person1, Person person2)
{
if (person1 is null)
{
return person2 is not null;
}
return !person1.Equals(person2);
}
}
open System
type Person(lastName: string, nationalId: string) =
member this.LastName = lastName
member this.NationalId = nationalId
interface IEquatable with
member this.Equals(other: Person) =
other.NationalId = this.NationalId
override this.Equals(obj: obj) =
match obj with
| :? Person as person -> (this :> IEquatable).Equals(person)
| _ -> false
override this.GetHashCode() =
this.NationalId.GetHashCode()
static member (==) (person1: Person, person2: Person) =
person1.Equals(person2)
static member (!=) (person1: Person, person2: Person) =
not (person1.Equals(person2))
Public Class Person
Implements IEquatable(Of Person)
Public Sub New(lastName As String, nationalId As String)
Me.LastName = lastName
Me.NationalId = nationalId
End Sub
Public ReadOnly Property LastName As String
Public ReadOnly Property NationalId As String
Public Overloads Function Equals(other As Person) As Boolean Implements IEquatable(Of Person).Equals
Return other IsNot Nothing AndAlso other.NationalId = Me.NationalId
End Function
Public Overrides Function Equals(obj As Object) As Boolean
Return Equals(TryCast(obj, Person))
End Function
Public Overrides Function GetHashCode() As Integer
Return NationalId.GetHashCode()
End Function
Public Shared Operator =(person1 As Person, person2 As Person) As Boolean
If person1 Is Nothing Then
Return person2 Is Nothing
End If
Return person1.Equals(person2)
End Operator
Public Shared Operator <>(person1 As Person, person2 As Person) As Boolean
If person1 Is Nothing Then
Return person2 IsNot Nothing
End If
Return Not person1.Equals(person2)
End Operator
End Class
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.
Note
The IComparable
The IEquatableContains, IndexOf, LastIndexOf, and Remove. It should be implemented for any object that might be stored in a generic collection.
For more information about implementing the IEquatable
Notes to Implementers
Replace the type parameter of the IEquatable
If you implement IEquatableEquals(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.
For information on overriding Equals(Object), see Equals(Object).
For a value type, you should always implement IEquatable
If you implement IEquatable
Note that there are some designs where a type supports an order relation, but equality may be distinct from an ordering relation. Consider a `Person` class where you sort alphabetically. Two people with the same name sort the same, but are not the same person.
Methods
| Name | Description |
|---|---|
| Equals(T) |
Indicates whether the current object is equal to another object of the same type. |