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

Definizione

Restituisce un valore che indica se l'oggetto Tuple<T1,T2,T3,T4> corrente è uguale a un oggetto specificato in base a un metodo di confronto specificato.

 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

Parametri

other
Object

Oggetto da confrontare con questa istanza.

comparer
IEqualityComparer

Oggetto che definisce il metodo da usare per valutare se i due oggetti sono uguali.

Restituisce

Boolean

true se l'istanza corrente è uguale all'oggetto specificato; in caso contrario, false.

Implementazioni

Esempio

L'esempio seguente definisce una Item3And4Comparer classe che implementa l'interfaccia e modifica il modo in cui Tuple<T1,T2,T3,T4> gli oggetti vengono valutati per l'uguaglianzaIEqualityComparer. Il metodo restituisce true sempre quando viene passato i Item1 valori delle proprietà e Item2 di due Tuple<T1,T2,T3,T4> oggetti e chiama il obj.Equals metodo per valutare Item3 i valori delle proprietà. Questa chiamata al metodo restituisce true, chiama anche il obj.Equals metodo per valutare i valori delle proprietà della Item4 tupla. Di conseguenza, il metodo verifica l'uguaglianza in base solo ai valori delle Item3 proprietà e Item4 . L'output illustra il risultato di un set di dati di oggetti che registra il nome di Tuple<T1,T2,T3,T4> una città degli Stati Uniti, il mese di un anno e la temperatura media elevata e bassa per quel mese.

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

Commenti

Questo membro è un’implementazione esplicita di un membro di interfaccia. Può essere utilizzato solo quando si esegue il cast dell'istanza Tuple<T1,T2,T3,T4> a un'interfaccia IStructuralEquatable.

L'implementazione IEqualityComparer.Equals viene chiamata solo se other non nullè , e se può essere eseguita correttamente il cast (in C#) o la conversione (in Visual Basic) in un Tuple<T1,T2,T3,T4> oggetto i cui componenti sono degli stessi tipi dell'istanza corrente. Il IStructuralEquatable.Equals(Object, IEqualityComparer) metodo passa prima i Item1 valori degli Tuple<T1,T2,T3,T4> oggetti da confrontare con l'implementazione IEqualityComparer.Equals . Se questa chiamata al metodo restituisce true, il metodo viene chiamato di nuovo e passato i Item2 valori dei due Tuple<T1,T2,T3,T4> oggetti. Se questa chiamata al metodo restituisce true di nuovo, il metodo viene chiamato una terza volta e passato i Item3 valori dei due Tuple<T1,T2,T3,T4> oggetti. Se questa chiamata al metodo restituisce true di nuovo, il metodo viene chiamato per la quarta e ultima volta e passato i Item4 valori dei due Tuple<T1,T2,T3,T4> oggetti.

Si applica a