Tuple<T1,T2,T3,T4>.IStructuralEquatable.Equals Metoda

Definicja

Zwraca wartość wskazującą, czy bieżący Tuple<T1,T2,T3,T4> obiekt jest równy określonemu obiektowi na podstawie określonej metody porównania.

 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

Parametry

other
Object

Obiekt, który ma zostać porównany z tym wystąpieniem.

comparer
IEqualityComparer

Obiekt definiujący metodę służącą ocenie, czy dwa obiekty są sobie równe.

Zwraca

Boolean

true jeśli bieżące wystąpienie jest równe określonemu obiektowi; w przeciwnym razie , false.

Implementuje

Przykłady

W poniższym przykładzie zdefiniowano klasę Item3And4Comparer , która implementuje IEqualityComparer interfejs i zmienia sposób Tuple<T1,T2,T3,T4> oceniania obiektów pod kątem równości. Metoda zawsze zwraca true wartość , gdy jest przekazywana Item1 wartości właściwości i Item2 dwóch Tuple<T1,T2,T3,T4> obiektów, i wywołuje metodę obj.Equals w celu oceny ich Item3 wartości właściwości. To wywołanie metody zwraca wartość true, wywołuje obj.Equals również metodę w celu oceny wartości właściwości krotek Item4 . W rezultacie metoda sprawdza równość tylko na podstawie wartości Item3 właściwości i Item4 . Dane wyjściowe ilustrują wynik zestawu Tuple<T1,T2,T3,T4> danych obiektów, które rejestrują nazwę miasta USA, miesiąc roku oraz średnią wysoką i niską temperaturę dla tego miesiąca.

using System;
using System.Collections;

public class Item3And4Comparer<T1, T2, T3, T4> : IEqualityComparer
{
   private int argument = 0;
   
   new public bool Equals(object x, object y)
   {
      argument++;
      
      // Return true for all values of Item1, Item2.
      if (argument <= 2)
         return true;
      else
         return x.Equals(y);
   }
   
   public int GetHashCode(object obj)
   {
      if (obj is T1)
         return ((T1) obj).GetHashCode();
      else if (obj is T2)
         return ((T2) obj).GetHashCode();
      else if (obj is T3)
         return ((T3) obj).GetHashCode();
      else
         return ((T4) obj).GetHashCode();
   }
}

public class Example
{
   public static void Main()
   {
      Tuple<string, int, double, double>[] temperatures = 
            { Tuple.Create("New York, NY", 4, 61.0, 43.0),
              Tuple.Create("Chicago, IL", 2, 34.0, 18.0), 
              Tuple.Create("Newark, NJ", 4, 61.0, 43.0),
              Tuple.Create("Boston, MA", 6, 77.0, 59.0),
              Tuple.Create("Detroit, MI", 9, 74.0, 53.0),
              Tuple.Create("Minneapolis, MN", 8, 81.0, 61.0) }; 
      // Compare each item with every other item for equality.
      for (int ctr = 0; ctr < temperatures.Length; ctr++)
      {
         IStructuralEquatable temperatureInfo = temperatures[ctr];
         for (int ctr2 = ctr + 1; ctr2 < temperatures.Length; ctr2++)
            Console.WriteLine("{0} = {1}: {2}", 
                              temperatureInfo, temperatures[ctr2], 
                              temperatureInfo.Equals(temperatures[ctr2], 
                                              new Item3And4Comparer<string, int, double, double>()));

         Console.WriteLine();                                               
      }
   }
}
// The example displays the following output:
//    (New York, NY, 4, 61, 43) = (Chicago, IL, 2, 34, 18): False
//    (New York, NY, 4, 61, 43) = (Newark, NJ, 4, 61, 43): True
//    (New York, NY, 4, 61, 43) = (Boston, MA, 6, 77, 59): False
//    (New York, NY, 4, 61, 43) = (Detroit, MI, 9, 74, 53): False
//    (New York, NY, 4, 61, 43) = (Minneapolis, MN, 8, 81, 61): False
//    
//    (Chicago, IL, 2, 34, 18) = (Newark, NJ, 4, 61, 43): False
//    (Chicago, IL, 2, 34, 18) = (Boston, MA, 6, 77, 59): False
//    (Chicago, IL, 2, 34, 18) = (Detroit, MI, 9, 74, 53): False
//    (Chicago, IL, 2, 34, 18) = (Minneapolis, MN, 8, 81, 61): False
//    
//    (Newark, NJ, 4, 61, 43) = (Boston, MA, 6, 77, 59): False
//    (Newark, NJ, 4, 61, 43) = (Detroit, MI, 9, 74, 53): False
//    (Newark, NJ, 4, 61, 43) = (Minneapolis, MN, 8, 81, 61): False
//    
//    (Boston, MA, 6, 77, 59) = (Detroit, MI, 9, 74, 53): False
//    (Boston, MA, 6, 77, 59) = (Minneapolis, MN, 8, 81, 61): False
//    
//    (Detroit, MI, 9, 74, 53) = (Minneapolis, MN, 8, 81, 61): False
open System
open System.Collections

type Item3And4Comparer<'T1, 'T2, 'T3, 'T4 when 'T1: equality and 'T2: equality and 'T3: equality and 'T4: equality>() =
    let mutable argument = 0
    interface IEqualityComparer with
        member _.Equals(x: obj, y: obj) =
            argument <- argument + 1
            
            // Return true for all values of Item1, Item2.
            if argument <= 2 then
                true
            else
                x.Equals y
        
        member _.GetHashCode(obj: obj) =
            match obj with
            | :? 'T1 as obj ->
                obj.GetHashCode()
            | :? 'T2 as obj ->
                obj.GetHashCode()
            | :? 'T3 as obj ->
                obj.GetHashCode()
            | _ ->
                (obj :?> 'T4).GetHashCode()

let temperatures = 
    [| Tuple.Create("New York, NY", 4, 61.0, 43.0)
       Tuple.Create("Chicago, IL", 2, 34.0, 18.0) 
       Tuple.Create("Newark, NJ", 4, 61.0, 43.0)
       Tuple.Create("Boston, MA", 6, 77.0, 59.0)
       Tuple.Create("Detroit, MI", 9, 74.0, 53.0)
       Tuple.Create("Minneapolis, MN", 8, 81.0, 61.0) |]
// Compare each item with every other item for equality.
for ctr = 0 to temperatures.Length - 1 do
    let temperatureInfo: IStructuralEquatable = temperatures[ctr]
    for ctr2 = ctr + 1 to temperatures.Length - 1 do
        printfn $"{temperatureInfo} = {temperatures[ctr2]}: {temperatureInfo.Equals(temperatures[ctr2], Item3And4Comparer<string, int, double, double>())}"
    printfn ""                                               
// The example displays the following output:
//    (New York, NY, 4, 61, 43) = (Chicago, IL, 2, 34, 18): False
//    (New York, NY, 4, 61, 43) = (Newark, NJ, 4, 61, 43): True
//    (New York, NY, 4, 61, 43) = (Boston, MA, 6, 77, 59): False
//    (New York, NY, 4, 61, 43) = (Detroit, MI, 9, 74, 53): False
//    (New York, NY, 4, 61, 43) = (Minneapolis, MN, 8, 81, 61): False
//    
//    (Chicago, IL, 2, 34, 18) = (Newark, NJ, 4, 61, 43): False
//    (Chicago, IL, 2, 34, 18) = (Boston, MA, 6, 77, 59): False
//    (Chicago, IL, 2, 34, 18) = (Detroit, MI, 9, 74, 53): False
//    (Chicago, IL, 2, 34, 18) = (Minneapolis, MN, 8, 81, 61): False
//    
//    (Newark, NJ, 4, 61, 43) = (Boston, MA, 6, 77, 59): False
//    (Newark, NJ, 4, 61, 43) = (Detroit, MI, 9, 74, 53): False
//    (Newark, NJ, 4, 61, 43) = (Minneapolis, MN, 8, 81, 61): False
//    
//    (Boston, MA, 6, 77, 59) = (Detroit, MI, 9, 74, 53): False
//    (Boston, MA, 6, 77, 59) = (Minneapolis, MN, 8, 81, 61): False
//    
//    (Detroit, MI, 9, 74, 53) = (Minneapolis, MN, 8, 81, 61): False
Imports System.Collections

Public Class Item3And4Comparer(Of T1, T2, T3, T4) : Implements IEqualityComparer
   
   Private argument As Integer = 0
   
   Public Overloads Function Equals(x As Object, y As Object) As Boolean _
                   Implements IEqualityComparer.Equals
      argument += 1
      
      ' Return true for all values of Item1, Item2.
      If argument <= 2 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()
      ElseIf TypeOf obj Is T2 Then
         Return CType(obj, T2).GetHashCode()
      ElseIf TypeOf obj Is T3 Then
         REturn CType(Obj, T3).GetHashCode()
      Else	
         Return CType(obj, T4).GetHashCode()
      End If
   End Function                
End Class

Module Example
   Public Sub Main()
      Dim temperatures() = 
              { Tuple.Create("New York, NY", 4, 61, 43), _
                Tuple.Create("Chicago, IL", 2, 34, 18), _ 
                Tuple.Create("Newark, NJ", 4, 61, 43), _
                Tuple.Create("Boston, MA", 6, 77, 59), _
                Tuple.Create("Detroit, MI", 9, 74, 53), _
                Tuple.Create("Minneapolis, MN", 8, 81, 61) } 
      ' Compare each item with every other item for equality.
      For ctr As Integer = 0 To temperatures.Length - 1
         Dim temperatureInfo As IStructuralEquatable = temperatures(ctr)
         For ctr2 As Integer = ctr + 1 To temperatures.Length - 1
            Console.WriteLine("{0} = {1}: {2}", 
                              temperatureInfo, temperatures(ctr2), 
                              temperatureInfo.Equals(temperatures(ctr2), 
                                              New Item3And4Comparer(Of String, Integer, Double, Double)))
         Next  
         Console.WriteLine()                                               
      Next
   End Sub
End Module
' The example displays the following output:
'    (New York, NY, 4, 61, 43) = (Chicago, IL, 2, 34, 18): False
'    (New York, NY, 4, 61, 43) = (Newark, NJ, 4, 61, 43): True
'    (New York, NY, 4, 61, 43) = (Boston, MA, 6, 77, 59): False
'    (New York, NY, 4, 61, 43) = (Detroit, MI, 9, 74, 53): False
'    (New York, NY, 4, 61, 43) = (Minneapolis, MN, 8, 81, 61): False
'    
'    (Chicago, IL, 2, 34, 18) = (Newark, NJ, 4, 61, 43): False
'    (Chicago, IL, 2, 34, 18) = (Boston, MA, 6, 77, 59): False
'    (Chicago, IL, 2, 34, 18) = (Detroit, MI, 9, 74, 53): False
'    (Chicago, IL, 2, 34, 18) = (Minneapolis, MN, 8, 81, 61): False
'    
'    (Newark, NJ, 4, 61, 43) = (Boston, MA, 6, 77, 59): False
'    (Newark, NJ, 4, 61, 43) = (Detroit, MI, 9, 74, 53): False
'    (Newark, NJ, 4, 61, 43) = (Minneapolis, MN, 8, 81, 61): False
'    
'    (Boston, MA, 6, 77, 59) = (Detroit, MI, 9, 74, 53): False
'    (Boston, MA, 6, 77, 59) = (Minneapolis, MN, 8, 81, 61): False
'    
'    (Detroit, MI, 9, 74, 53) = (Minneapolis, MN, 8, 81, 61): False

Uwagi

Ten element jest jawną implementacją członków. Można go używać tylko wtedy, gdy Tuple<T1,T2,T3,T4> wystąpienie jest rzutowanie do interfejsu IStructuralEquatable .

Implementacja IEqualityComparer.Equals jest wywoływana tylko wtedy, gdy other nie nulljest , a jeśli można ją pomyślnie rzutować (w języku C#) lub przekonwertować (w Visual Basic) na Tuple<T1,T2,T3,T4> obiekt, którego składniki są tego samego typu co bieżące wystąpienie. Metoda IStructuralEquatable.Equals(Object, IEqualityComparer) najpierw przekazuje Item1 wartości Tuple<T1,T2,T3,T4> obiektów do porównania z implementacją IEqualityComparer.Equals . Jeśli to wywołanie metody zwróci truemetodę , metoda zostanie wywołana ponownie i przekazana Item2 wartości dwóch Tuple<T1,T2,T3,T4> obiektów. Jeśli to wywołanie metody zostanie zwrócone true ponownie, metoda zostanie wywołana po raz trzeci i przekazana Item3 wartości dwóch Tuple<T1,T2,T3,T4> obiektów. Jeśli to wywołanie metody zwróci true ponownie, metoda jest wywoływana dla czwartego i ostatniego czasu i przekazała Item4 wartości dwóch Tuple<T1,T2,T3,T4> obiektów.

Dotyczy