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.

LinkedList<T> List<T> Dictionary<TKey,TValue>當在LastIndexOf IEquatable<T>Contains 、和Remove這類方法中測試是否相等時, 泛型集合物件 (例如、和) 會使用介面。 IndexOfThe 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_Equality多載和op_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.

適用於

另請參閱