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)

類型參數

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.

ContainsIndexOfLastIndexOfRemove等方法中測試是否相等時,泛型集合物件(例如 Dictionary<TKey,TValue>List<T>LinkedList<T>)會使用 IEquatable<T> 介面。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.

方法

Equals(T)

指出目前的物件是否等於另一個相同類型的物件。Indicates whether the current object is equal to another object of the same type.

適用於

另請參閱