IEquatable<T> IEquatable<T> IEquatable<T> IEquatable<T> Interface


定义由值类型或类实现的通用方法,旨在创建特定于类型的方法以确定实例的质量。Defines a generalized method that a value type or class implements to create a type-specific method for determining equality of instances.

generic <typename T>
public interface class IEquatable
public interface IEquatable<T>
type IEquatable<'T> = interface
Public Interface IEquatable(Of T)



要比较的对象的类型。The type of objects to compare.



有关示例,请参阅IEquatable<T>.Equals方法。See the example for the IEquatable<T>.Equals method.


此接口实现由其值可以等同 (对于示例、 数字和字符串类) 的类型。This interface is implemented by types whose values can be equated (for example, the numeric and string classes). 值类型或类实现Equals方法来创建特定于类型的方法适用于确定实例的质量。A value type or class implements the Equals method to create a type-specific method suitable for determining equality of instances.


IComparable<T>接口定义CompareTo方法,用于确定的实现类型实例的排序顺序。The IComparable<T> interface defines the CompareTo method, which determines the sort order of instances of the implementing type. IEquatable<T>接口定义Equals方法,这可确定的相等性的实现类型的实例。The IEquatable<T> interface defines the Equals method, which determines the equality of instances of the implementing type.

IEquatable<T>接口如由泛型集合对象Dictionary<TKey,TValue>List<T>,和LinkedList<T>作为此类方法中的相等性测试时ContainsIndexOfLastIndexOf,和RemoveThe IEquatable<T> interface is used by generic collection objects such as Dictionary<TKey,TValue>, List<T>, and LinkedList<T> 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.


替换为的类型参数IEquatable<T>接口与实现此接口的类型。Replace the type parameter of the IEquatable<T> interface with the type that is implementing this interface. 如果你实现了IEquatable<T>,还应该重写的基类实现Equals(Object)GetHashCode(),使其行为与保持一致Equals(T)方法。If you implement IEquatable<T>, you should also override the base class implementations of Equals(Object) and GetHashCode() so that their behavior is consistent with that of the Equals(T) method. 如果你重写Equals(Object),重写的实现也称为中调用静态Equals(System.Object, System.Object)在类上的方法。If you do override Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. 此外,您应重载op_Equalityop_Inequality运算符。In addition, you should overload the op_Equality and op_Inequality operators. 这可确保所有测试相等,都返回一致的结果。This ensures that all tests for equality return consistent results.

有关信息重写Equals(Object),请参阅Equals(Object)一文。For information on overriding Equals(Object), see the Equals(Object) article.

对于值类型,您应该始终实现IEquatable<T>并重写Equals(Object)以提高性能。For a value type, you should always implement IEquatable<T> and override Equals(Object) for better performance. Equals(Object) 框的值类型和依赖于反射来比较两个值相等。Equals(Object) boxes value types and relies on reflection to compare two values for equality. 这两个实现的Equals(T)的重写和Equals(Object)应返回一致的结果。Both your implementation of Equals(T) and your override of Equals(Object) should return consistent results. 如果你实现了IEquatable<T>,还应实现IComparable<T>如果类型的实例可以有序或排序。If you implement IEquatable<T>, you should also implement IComparable<T> if instances of your type can be ordered or sorted. 如果您的类型实现IComparable<T>,几乎总是还实现IEquatable<T>If your type implements IComparable<T>, you almost always also implement IEquatable<T>.

请注意,有一些设计其中某个类型支持的顺序关系,但可能不同于排序关系相等。Note that there are some designs where a type supports an order relation, but equality may be distinct from an ordering relation. 请考虑`Person`类,其中按字母顺序排序。Consider a `Person` class where you sort alphabetically. 具有相同名称的两名人员排序相同,但不是同一个人。Two people with the same name sort the same, but are not the same person.


Equals(T) Equals(T) Equals(T) Equals(T)

指示当前对象是否等于同一类型的另一个对象。Indicates whether the current object is equal to another object of the same type.