IStructuralEquatable IStructuralEquatable IStructuralEquatable 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 接口的 IStructuralEquatable 类。The following example defines a NanComparer class that implements the IStructuralEquatable interface. 它比较两个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;
      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 元组对象的组件中包含的三个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.
      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. 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 类 (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接口值并提供你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) Equals(Object, IEqualityComparer) Equals(Object, IEqualityComparer) Equals(Object, IEqualityComparer)

确定某个对象与当前实例在结构上是否相等。Determines whether an object is structurally equal to the current instance.

GetHashCode(IEqualityComparer) GetHashCode(IEqualityComparer) GetHashCode(IEqualityComparer) GetHashCode(IEqualityComparer)

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

适用于

另请参阅