IEquatable<T>.Equals(T) メソッド

定義

現在のオブジェクトが同じ型の別のオブジェクトと等しいかどうかを示します。

public:
 bool Equals(T other);
public bool Equals(T other);
public bool Equals(T? other);
abstract member Equals : 'T -> bool
Public Function Equals (other As T) As Boolean

パラメーター

other
T

このオブジェクトと比較するオブジェクト。

返品

true 現在のオブジェクトが other パラメーターと等しい場合は false。それ以外の場合は。

次の例は、Personを実装し、IEquatable<T>LastNameの 2 つのプロパティを持つNationalId クラスの部分的な実装を示しています。 NationalIdは一意識別子と見なされるため、Equals メソッドは、2 つのTrue オブジェクトのNationalId プロパティが同一の場合はPersonを返します。それ以外の場合は、Falseを返します。 (F# の例では、null インスタンスのPerson値は処理されないことに注意してください)。

public class Person : IEquatable<Person>
{
    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<Person> with
        member this.Equals(other: Person) =
            other.NationalId = this.NationalId

    override this.Equals(obj: obj) =
        match obj with
        | :? Person as person -> (this :> IEquatable<Person>).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

PersonList<T>に格納されている場合、ContainsはそのEquals実装を使用して一致を検索します。

List<Person> applicants = new List<Person>()
{
    new Person("Jones", "099-29-4999"),
    new Person("Jones", "199-29-3999"),
    new Person("Jones", "299-49-6999")
};

// Create a Person object for the final candidate.
Person candidate = new Person("Jones", "199-29-3999");
bool contains = applicants.Contains(candidate);
Console.WriteLine($"{candidate.LastName} ({candidate.NationalId}) is on record: {contains}");
// The example prints the following output:
// Jones (199-29-3999) is on record: True
let applicants = 
    [ Person("Jones", "099-29-4999")
      Person("Jones", "199-29-3999")
      Person("Jones", "299-49-6999") ]

let candidate = Person("Jones", "199-29-3999")
let contains = List.contains candidate applicants
printfn "%s (%s) is on record: %b" candidate.LastName candidate.NationalId contains
// The example prints the following output:
// Jones (199-29-3999) is on record: true
Dim applicants As New List(Of Person)
applicants.Add(New Person("Jones", "099-29-4999"))
applicants.Add(New Person("Jones", "199-29-3999"))
applicants.Add(New Person("Jones", "299-49-6999"))

' Create a Person object for the final candidate.
Dim candidate As New Person("Jones", "199-29-3999")
Dim contains As Boolean = applicants.Contains(candidate)
Console.WriteLine($"{candidate.LastName} ({candidate.NationalId}) is on record: {contains}")
' The example prints the following output:
' Jones (199-29-3999) Is on record True

注釈

Equals メソッドの実装は、現在のオブジェクトと同じ型である、T型の別のオブジェクトとの等価性のテストを実行することを目的としています。 Equals(T) メソッドは、次の状況で呼び出されます。

つまり、クラスのオブジェクトが配列またはジェネリック コレクション オブジェクトに格納される可能性を処理するには、オブジェクトを簡単に識別して操作できるように、 IEquatable<T> を実装することをお勧めします。

Equals メソッドを実装する場合は、ジェネリック型引数で指定された型に対して等価性を適切に定義します。 たとえば、型引数が Int32の場合は、2 つの 32 ビット符号付き整数の比較に対して等価性を適切に定義します。

注意 (実装者)

Equals(T)を実装する場合は、Equals(Object)GetHashCode()の基底クラスの実装をオーバーライドして、その動作がEquals(T)メソッドの基底クラスの実装と一致するようにする必要もあります。 Equals(Object)をオーバーライドする場合、オーバーライドされた実装は、クラスの静的Equals(System.Object, System.Object) メソッドの呼び出しでも呼び出されます。 さらに、 op_Equality 演算子と op_Inequality 演算子をオーバーロードする必要があります。 これにより、等価性のすべてのテストで一貫した結果が返されることが保証されます。この例で示します。

適用対象