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)

类型参数

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.

Dictionary<TKey,TValue> List<T> LinkedList<T>当在IEquatable<T>IndexOf、和Remove等方法中测试相等性时, 泛型集合对象 (如、和) 使用接口。 LastIndexOf ContainsThe 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.

适用于

另请参阅