IEquatable<T>.Equals(T) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指示当前对象是否等于同一类型的另一个对象。
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
要与此对象进行比较的对象。
返回
示例
下面的示例演示实现Person并具有两个IEquatable<T>属性的类的部分实现。 LastNameNationalId
NationalId被视为唯一标识符,因此,Equals如果True两NationalId个对象的属性相同,则该方法返回PersonFalse;否则返回。
(请注意,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
当 a Person 存储在 a List<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) 以下情况下调用该方法:
Equals调用该方法时,other参数是类型的T强类型对象。 (如果other不是类型T,则调用基 Object.Equals(Object) 方法。 在两种方法中, IEquatable<T>.Equals 提供略微更好的性能。调用多个泛型集合对象的搜索方法时。 其中一些类型及其方法包括:
方法的 BinarySearch 一些泛型重载。
类的List<T>搜索方法,包括List<T>.Contains(T)、List<T>.IndexOf和List<T>.LastIndexOfList<T>.Remove。
类的 Dictionary<TKey,TValue> 搜索方法,包括 ContainsKey 和 Remove。
泛型 LinkedList<T> 类的搜索方法,包括 LinkedList<T>.Contains 和 Remove。
换句话说,为了处理类的对象将存储在数组或泛型集合对象中的可能性,最好实现 IEquatable<T> 该对象,以便可以轻松识别和操作对象。
实现 Equals 该方法时,请适当地为泛型类型参数指定的类型定义相等性。 例如,如果类型参数为 Int32,则适当地定义两个 32 位有符号整数的比较相等性。
实施者说明
如果实现Equals(T),还应重写其基类实现Equals(Object)GetHashCode(),使其行为与Equals(T)该方法的行为一致。 如果确实重写 Equals(Object),则重写的实现也会在对类上的静态 Equals(System.Object, System.Object) 方法的调用中调用。 此外,还应重载 op_Equality 和 op_Inequality 运算符。 这可确保所有相等性测试都返回一致的结果,本示例对此进行了说明。