Uredi

Deli z drugimi prek


IStructuralEquatable.Equals(Object, IEqualityComparer) Method

Definition

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);
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

Parameters

other
Object

The object to compare with the current instance.

comparer
IEqualityComparer

An object that determines whether the current instance and other are equal.

Returns

true if the two objects are equal; otherwise, false.

Examples

The default equality comparer, EqualityComparer<Object>.Default.Equals, considers two NaN values to be equal. 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. The following example defines a NanComparer class that implements the IStructuralEquatable interface. 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

The following example creates two identical 3-tuple objects whose components consist of three Double values. The value of the second component is Double.NaN. The example then calls the Tuple<T1,T2,T3>.Equals method, and it calls the IStructuralEquatable.Equals method three times. The first time, it passes the default equality comparer that is returned by the EqualityComparer<T>.Default property. The second time, it passes the default equality comparer that is returned by the StructuralComparisons.StructuralEqualityComparer property. The third time, it passes the custom NanComparer object. As 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

Remarks

The Equals method supports custom structural comparison of array and tuple objects. 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. 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.

Applies to

See also