IEquatable<T> 接口


定义由值类型或类实现的通用方法,旨在创建特定于类型的方法以确定实例的质量。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>.EqualsSee 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> 方法中测试相等性时, Contains IndexOf LastIndexOf 泛型集合 Remove 对象(如、和)使用接口。The 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.



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