IStructuralEquatable.Equals(Object, IEqualityComparer) 方法

定义

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

public:
 bool Equals(System::Object ^ other, System::Collections::IEqualityComparer ^ comparer);
public bool Equals (object other, System.Collections.IEqualityComparer comparer);
abstract member Equals : obj * System.Collections.IEqualityComparer -> bool
Public Function Equals (other As Object, comparer As IEqualityComparer) As Boolean

参数

other
Object

要与当前实例进行比较的对象。The object to compare with the current instance.

comparer
IEqualityComparer

一个对象,用于确定当前实例和 other 是否相等。An object that determines whether the current instance and other are equal.

返回

如果两个对象相等,则为 true;否则为 falsetrue if the two objects are equal; otherwise, false.

示例

默认的相等比较器 EqualityComparer<Object>.Default.Equals 会将两个 @no__t 1 值视为相等。The default equality comparer, EqualityComparer<Object>.Default.Equals, considers two NaN values to be equal. 但是,在某些情况下,您可能需要比较 @no__t 值的相等性以返回 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. 它使用相等运算符比较两个 @no__t 0 个或两个 @no__t 值。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 方法,并调用三次 @no__t 方法。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. 第三次,它传递自定义 @no__t 0 对象。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

注解

@No__t-0 方法支持数组和元组对象的自定义结构比较。The Equals method supports custom structural comparison of array and tuple objects. 此方法反过来调用 @no__t 0 对象的 IEqualityComparer.Equals 方法来比较单个数组元素或元组组件(从第一个元素或组件开始)。This method in turn calls the comparer object's IEqualityComparer.Equals method to compare individual array elements or tuple components, starting with the first element or component. 当方法调用返回 false 时,或在所有数组元素或元组组件进行了比较之后,单独调用 IEqualityComparer.Equals end 和 IStructuralEquatable.Equals 方法返回一个值。The individual calls to IEqualityComparer.Equals end and the IStructuralEquatable.Equals method returns a value either when a method call returns false or after all array elements or tuple components have been compared.

适用于

另请参阅