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 物件(例如、和)會使用此介面 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_Equality and 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) box 值型別並依賴反映來比較兩個值是否相等。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.