IStructuralEquatable Interface


定义用于支持比较对象是否在结构上相等的方法。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会将两个 NaN 值视为相等。The default equality comparer, EqualityComparer<Object>.Default.Equals, considers two NaN values to be equal. 但是,在某些情况下,您可能希望比较相等性 NaN 值以返回 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. 第三个示例使用它作为元组实现 IStructuralEquatable 接口的 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. 它使用相等运算符比较两个 Double 两个 Single 值或两个值。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;
         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)
         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元组对象,其组件由三个 Double 值组成。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.

      IStructuralEquatable equ = t1;
      // Call IStructuralEquatable.Equals using default comparer.
      Console.WriteLine(equ.Equals(t2, EqualityComparer<object>.Default));

      // Call IStructuralEquatable.Equals using
      // 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.
      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.
      ' 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. IStructuralEquatable 接口允许您实现自定义的比较,以检查集合对象的结构是否相等。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.


IStructuralEquatable 接口仅支持结构相等性的自定义比较。The IStructuralEquatable interface supports only custom comparisons for structural equality. IStructuralComparable 接口支持用于排序和排序的自定义结构比较。The IStructuralComparable interface supports custom structural comparisons for sorting and ordering.

.NET Framework 还提供了默认的相等比较器,由 EqualityComparer<T>.DefaultStructuralComparisons.StructuralEqualityComparer 属性返回。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>等)和 Array 类提供 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#元组的当前实例强制转换(在中)或转换(Visual Basic)到 IStructuralEquatable interface 值并将 IEqualityComparer 实现作为参数提供给 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.


返回当前实例的哈希代码。Returns a hash code for the current instance.

Applies to

See also