편집

다음을 통해 공유


Tuple<T1,T2>.IStructuralEquatable.Equals Method

Definition

Returns a value that indicates whether the current Tuple<T1,T2> object is equal to a specified object based on a specified comparison method.

 virtual bool System.Collections.IStructuralEquatable.Equals(System::Object ^ other, System::Collections::IEqualityComparer ^ comparer) = System::Collections::IStructuralEquatable::Equals;
bool IStructuralEquatable.Equals (object other, System.Collections.IEqualityComparer comparer);
abstract member System.Collections.IStructuralEquatable.Equals : obj * System.Collections.IEqualityComparer -> bool
override this.System.Collections.IStructuralEquatable.Equals : obj * System.Collections.IEqualityComparer -> bool
Function Equals (other As Object, comparer As IEqualityComparer) As Boolean Implements IStructuralEquatable.Equals

Parameters

other
Object

The object to compare with this instance.

comparer
IEqualityComparer

An object that defines the method to use to evaluate whether the two objects are equal.

Returns

true if the current instance is equal to the specified object; otherwise, false.

Implements

Examples

The following example defines an Item2Comparer class that implements the IEqualityComparer interface and changes the way in which Tuple<T1,T2> objects are evaluated for equality. The method always returns true when it is passed the Item1 property values of two Tuple<T1,T2> objects, and it calls the IStructuralEquatable.Equals method to evaluate their Item2 property values. As a result, the method tests for equality based only on the value of the Item2 property. The output illustrates the result for a data set of Tuple<T1,T2> objects that record the names of runners and the distances that they ran.

using System;
using System.Collections;

public class Item2Comparer<T1, T2> : IEqualityComparer
{
   new public bool Equals(object x, object y)
   {
      // Return true for all values of Item1.
      if (x is T1)
      //if (typeof(x) is string) 
         return true;
      else
         return x.Equals(y);
   }
   
   public int GetHashCode(object obj)
   {
      if (obj is T1)
         return ((T1) obj).GetHashCode();
      else
         return ((T2) obj).GetHashCode();
   }                
}

public class Example
{
   public static void Main()
   {
      Tuple<string, double>[] distancesWalked = {
                        Tuple.Create("Jan", Double.NaN), 
                        Tuple.Create("Joe", Double.NaN), 
                        Tuple.Create("Adam", 1.36), 
                        Tuple.Create("Selena", 2.01),
                        Tuple.Create("Jake", 1.36) };
      for (int ctr = 0; ctr < distancesWalked.Length; ctr++)
      {
         Tuple<string, double> distanceWalked = distancesWalked[ctr];
         for (int ctr2 = ctr + 1; ctr2 < distancesWalked.Length; ctr2++)
         {
            Console.WriteLine("{0} = {1}: {2}", distanceWalked, 
                              distancesWalked[ctr2], 
                              ((IStructuralEquatable)distanceWalked).Equals(distancesWalked[ctr2], 
                                                    new Item2Comparer<string, double>()));
         }
         Console.WriteLine();
      }   
   }
}
// The example displays the following output:
//       (Jan, NaN) = (Joe, NaN): True
//       (Jan, NaN) = (Adam, 1.36): False
//       (Jan, NaN) = (Selena, 2.01): False
//       (Jan, NaN) = (Jake, 1.36): False
//       
//       (Joe, NaN) = (Adam, 1.36): False
//       (Joe, NaN) = (Selena, 2.01): False
//       (Joe, NaN) = (Jake, 1.36): False
//       
//       (Adam, 1.36) = (Selena, 2.01): False
//       (Adam, 1.36) = (Jake, 1.36): True
//       
//       (Selena, 2.01) = (Jake, 1.36): False
open System
open System.Collections

type Item2Comparer<'T1, 'T2 when 'T1: equality and 'T2: equality>() = 
    interface IEqualityComparer with    
        member _.GetHashCode(obj) =
            match obj with
            | :? 'T1 as obj->
                obj.GetHashCode()
            | _ ->
                (obj :?> 'T2).GetHashCode()

        member _.Equals(x, y) =
            // Return true for all values of Item1.
            match x with
            | :? 'T1 ->
                true
            | _ ->
                x.Equals y

let distancesWalked =
    [| Tuple.Create("Jan", Double.NaN) 
       Tuple.Create("Joe", Double.NaN) 
       Tuple.Create("Adam", 1.36)
       Tuple.Create("Selena", 2.01)
       Tuple.Create("Jake", 1.36) |]

for ctr = 0 to distancesWalked.Length - 1 do
    let distanceWalked = distancesWalked[ctr]
    for ctr2 = ctr + 1 to distancesWalked.Length - 1 do
        printfn $"{distanceWalked} = {distancesWalked[ctr2]}: {(distanceWalked :> IStructuralEquatable).Equals(distancesWalked[ctr2], Item2Comparer<string, double>())}"
    printfn ""
// The example displays the following output:
//       (Jan, NaN) = (Joe, NaN): True
//       (Jan, NaN) = (Adam, 1.36): False
//       (Jan, NaN) = (Selena, 2.01): False
//       (Jan, NaN) = (Jake, 1.36): False
//       
//       (Joe, NaN) = (Adam, 1.36): False
//       (Joe, NaN) = (Selena, 2.01): False
//       (Joe, NaN) = (Jake, 1.36): False
//       
//       (Adam, 1.36) = (Selena, 2.01): False
//       (Adam, 1.36) = (Jake, 1.36): True
//       
//       (Selena, 2.01) = (Jake, 1.36): False
Imports System.Collections

Public Class Item2Comparer(Of T1, T2) : Implements IEqualityComparer
   
   Public Overloads Function Equals(x As Object, y As Object) As Boolean _
                   Implements IEqualityComparer.Equals
      ' Return true for all values of Item1.
      If TypeOf x Is T1 Then
         Return True
      Else
         Return x.Equals(y)
      End If
   End Function
   
   Public Overloads Function GetHashCode(obj As Object) As Integer _
                    Implements IEqualityComparer.GetHashCode
      If TypeOf obj Is T1 Then
         Return CType(obj, T1).GetHashCode()
      Else
         Return CType(obj, T2).GetHashCode()
      End If   
   End Function                
End Class

Module Example
   Public Sub Main()
      Dim distancesWalked() = {
                        Tuple.Create("Jan", Double.NaN), 
                        Tuple.Create("Joe", Double.NaN), 
                        Tuple.Create("Adam", 1.36), 
                        Tuple.Create("Selena", 2.01),
                        Tuple.Create("Jake", 1.36) }
      For ctr As Integer = 0 To distancesWalked.Length - 1
         Dim distanceWalked As Tuple(Of String, Double) = distancesWalked(ctr)
         For ctr2 As Integer = ctr + 1 To distancesWalked.Length - 1
            Console.WriteLine("{0} = {1}: {2}", distanceWalked, 
                              distancesWalked(ctr2), 
                              DirectCast(distanceWalked, IStructuralEquatable).Equals(distancesWalked(ctr2), 
                                                    new Item2Comparer(Of String, Double)))
         Next
         Console.WriteLine()
      Next                     
   End Sub
End Module
' The example displays the following output:
'       (Jan, NaN) = (Joe, NaN): True
'       (Jan, NaN) = (Adam, 1.36): False
'       (Jan, NaN) = (Selena, 2.01): False
'       (Jan, NaN) = (Jake, 1.36): False
'       
'       (Joe, NaN) = (Adam, 1.36): False
'       (Joe, NaN) = (Selena, 2.01): False
'       (Joe, NaN) = (Jake, 1.36): False
'       
'       (Adam, 1.36) = (Selena, 2.01): False
'       (Adam, 1.36) = (Jake, 1.36): True
'       
'       (Selena, 2.01) = (Jake, 1.36): False

Remarks

This member is an explicit interface member implementation. It can be used only when the Tuple<T1,T2> instance is cast to an IStructuralEquatable interface.

The IStructuralEquatable.Equals implementation is called only if other is not null, and if it can be successfully cast (in C#) or converted (in Visual Basic) to a Tuple<T1,T2> object whose components are of the same types as the current instance. The IStructuralEquatable.Equals method first passes the Item1 values of the Tuple<T1,T2> objects to be compared to the IEqualityComparer.Equals implementation. If this method call returns true, the method is called again and passed the Item2 values of the two Tuple<T1,T2> objects.

Applies to

See also