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)



要比較之物件的類型。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.

IEquatable<T>介面由泛型集合物件這類Dictionary<TKey,TValue>List<T>,以及LinkedList<T>測試是否相等,在這種方法時ContainsIndexOfLastIndexOf,和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_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.