IStructuralEquatable 介面

定義

定義支援物件之結構相等比較的方法。Defines methods to support the comparison of objects for structural equality.

public interface class IStructuralEquatable
public interface IStructuralEquatable
type IStructuralEquatable = interface
Public Interface IStructuralEquatable
衍生

範例

預設的相等比較子 EqualityComparer<Object>.Default.Equals,會將兩個 @no__t 1 的值視為相等。The default equality comparer, EqualityComparer<Object>.Default.Equals, considers two NaN values to be equal. 不過,在某些情況下,您可能會想要將 @no__t 0 的值進行比較,以傳回 false,這表示無法比較這些值。However, in some cases, you may want the comparison of NaN values for equality to return false, which indicates that the values cannot be compared. 下列範例定義一個實作 NanComparer 介面的 IEqualityComparer 類別。The following example defines a NanComparer class that implements the IEqualityComparer interface. 第三個範例會使用它做為元組所執行之 @no__t 1 介面的 Equals(Object, IEqualityComparer) 方法的引數。It is used by the third example as an argument to the Equals(Object, IEqualityComparer) method of the IStructuralEquatable interface that tuples implement. 它會使用相等運算子來比較兩個 @no__t 0 或兩個 @no__t 1 的值。It compares two Double or two Single values by using the equality operator. 它會將任何其他類型的值傳遞至預設的相等比較子。It passes values of any other type to the default equality comparer.

using System;
using System.Collections;
using System.Collections.Generic;

public class NanComparer : IEqualityComparer
{
   public new bool Equals(object x, object y)
   {
      if (x is float)
         return (float) x == (float) y;
      else if (x is double)
         return (double) x == (double) y;
      else
         return EqualityComparer<object>.Default.Equals(x, y);
   }
   
   public int GetHashCode(object obj)
   {
      return EqualityComparer<object>.Default.GetHashCode(obj);
   }
}
Imports System.Collections
Imports System.Collections.Generic

Public Class NanComparer : Implements IEqualityComparer
   Public Overloads Function Equals(x As Object, y As Object) As Boolean _
          Implements IEqualityComparer.Equals
      If TypeOf x Is Single Then
         Return CSng(x) = CSng(y)
      ElseIf TypeOf x Is Double Then
         Return CDbl(x) = CDbl(y)
      Else
         Return EqualityComparer(Of Object).Default.Equals(x, y)
      End If
   End Function
   
   Public Overloads Function GetHashCode(obj As Object) As Integer _
          Implements IEqualityComparer.GetHashCode
      Return EqualityComparer(Of Object).Default.GetHashCode(obj)
   End Function
End Class

下列範例會建立兩個完全相同的3元組物件,其元件是由三個 @no__t 0 值所組成。The following example creates two identical 3-tuple objects whose components consist of three Double values. 第二個元件的值為 Double.NaNThe value of the second component is Double.NaN. 然後,此範例會呼叫 Tuple<T1,T2,T3>.Equals 方法,並呼叫 IStructuralEquatable.Equals 方法三次。The example then calls the Tuple<T1,T2,T3>.Equals method, and it calls the IStructuralEquatable.Equals method three times. 第一次,它會傳遞 EqualityComparer<T>.Default 屬性所傳回的預設相等比較子。The first time, it passes the default equality comparer that is returned by the EqualityComparer<T>.Default property. 第二次,它會傳遞 StructuralComparisons.StructuralEqualityComparer 屬性所傳回的預設相等比較子。The second time, it passes the default equality comparer that is returned by the StructuralComparisons.StructuralEqualityComparer property. 第三次,它會傳遞自訂的 NanComparer 物件。The third time, it passes the custom NanComparer object. 如範例的輸出所示,前三個方法呼叫會傳回 true,而第四個呼叫則會傳回 falseAs the output from the example shows, the first three method calls return true, whereas the fourth call returns false.

public class Example
{
   public static void Main()
   {
      var t1 = Tuple.Create(12.3, Double.NaN, 16.4);
      var t2 = Tuple.Create(12.3, Double.NaN, 16.4);
      
      // Call default Equals method.
      Console.WriteLine(t1.Equals(t2));
      
      IStructuralEquatable equ = t1;
      // Call IStructuralEquatable.Equals using default comparer.
      Console.WriteLine(equ.Equals(t2, EqualityComparer<object>.Default));
      
      // Call IStructuralEquatable.Equals using 
      // StructuralComparisons.StructuralEqualityComparer.
      Console.WriteLine(equ.Equals(t2, 
                        StructuralComparisons.StructuralEqualityComparer));
      
      // Call IStructuralEquatable.Equals using custom comparer.
      Console.WriteLine(equ.Equals(t2, new NanComparer()));
   }
}
// The example displays the following output:
//       True
//       True
//       True
//       False
Module Example
   Public Sub Main()
      Dim t1 = Tuple.Create(12.3, Double.NaN, 16.4)
      Dim t2 = Tuple.Create(12.3, Double.NaN, 16.4)
      
      ' Call default Equals method.
      Console.WriteLine(t1.Equals(t2))
      
      Dim equ As IStructuralEquatable = t1
      ' Call IStructuralEquatable.Equals using default comparer.
      Console.WriteLine(equ.Equals(t2, EqualityComparer(Of Object).Default))
      
      ' Call IStructuralEquatable.Equals using 
      ' StructuralComparisons.StructuralEqualityComparer.
      Console.WriteLine(equ.Equals(t2, 
                        StructuralComparisons.StructuralEqualityComparer))
      
      ' Call IStructuralEquatable.Equals using custom comparer.
      Console.WriteLine(equ.Equals(t2, New NanComparer))
   End Sub
End Module
' The example displays the following output:
'       True
'       True
'       True
'       False

備註

結構相等表示兩個物件相等,因為它們具有相等的值。Structural equality means that two objects are equal because they have equal values. 它與參考相等不同,這表示兩個物件參考相等,因為它們參考相同的實體物件。It differs from reference equality, which indicates that two object references are equal because they reference the same physical object. @No__t 0 介面可讓您執行自訂的比較,以檢查集合物件的結構是否相等。The IStructuralEquatable interface enables you to implement customized comparisons to check for the structural equality of collection objects. 也就是說,您可以建立自己的結構相等定義,並指定這個定義與接受 IStructuralEquatable 介面的集合型別搭配使用。That is, you can create your own definition of structural equality and specify that this definition be used with a collection type that accepts the IStructuralEquatable interface. 介面有兩個成員: Equals,這會使用指定的 IEqualityComparer 執行來測試是否相等,而 GetHashCode 則會針對相等的物件傳回相同的雜湊碼。The interface has two members: Equals, which tests for equality by using a specified IEqualityComparer implementation, and GetHashCode, which returns identical hash codes for objects that are equal.

注意

@No__t-0 介面僅支援結構相等的自訂比較。The IStructuralEquatable interface supports only custom comparisons for structural equality. @No__t 0 介面支援用於排序和排序的自訂結構比較。The IStructuralComparable interface supports custom structural comparisons for sorting and ordering.

.NET Framework 也會提供預設的相等比較子,由 EqualityComparer<T>.Default 和 @no__t 1 的屬性傳回。The .NET Framework also provides default equality comparers, which are returned by the EqualityComparer<T>.Default and StructuralComparisons.StructuralEqualityComparer properties. 如需詳細資訊,請參閱範例。For more information, see the example.

泛型元組類別(Tuple<T1>Tuple<T1,T2>Tuple<T1,T2,T3> 等等)和 @no__t 3 類別會提供 IStructuralEquatable 介面的明確實作為。The generic tuple classes (Tuple<T1>, Tuple<T1,T2>, Tuple<T1,T2,T3>, and so on) and the Array class provide explicit implementations of the IStructuralEquatable interface. 藉由將陣列C#或元組的目前實例轉型(in)或轉換(在 Visual Basic) @no__t 1 介面值,並提供您的 @no__t 2 實作為 Equals 方法的引數,您就可以定義陣列或集合。By casting (in C#) or converting (in Visual Basic) the current instance of an array or tuple to an IStructuralEquatable interface value and providing your IEqualityComparer implementation as an argument to the Equals method, you can define a custom equality comparison for the array or collection.

方法

Equals(Object, IEqualityComparer)

判斷物件與目前的執行個體在結構上是否相等。Determines whether an object is structurally equal to the current instance.

GetHashCode(IEqualityComparer)

傳回目前執行個體的雜湊碼。Returns a hash code for the current instance.

適用於

另請參閱