Tuple<T1,T2,T3,T4>.IStructuralEquatable.Equals Método

Definição

Retorna um valor que indica se o objeto Tuple<T1,T2,T3,T4> atual é igual ao objeto especificado com base em um determinado método de comparação.

 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

Parâmetros

other
Object

O objeto a ser comparado com essa instância.

comparer
IEqualityComparer

Um objeto que define o método a ser usado para avaliar se dois objetos são iguais.

Retornos

Boolean

true caso a instância atual seja igual ao objeto especificado; do contrário, false.

Implementações

Exemplos

O exemplo a seguir define uma Item3And4Comparer classe que implementa a IEqualityComparer interface e altera a maneira como Tuple<T1,T2,T3,T4> os objetos são avaliados quanto à igualdade. O método sempre retorna true quando é passado os Item1 valores de propriedade e Item2 dois Tuple<T1,T2,T3,T4> objetos e chama o obj.Equals método para avaliar seus Item3 valores de propriedade. Essa chamada de método retorna true, ela também chama o obj.Equals método para avaliar os valores de propriedade das Item4 tuplas. Como resultado, o método testa a igualdade com base apenas nos valores e Item3 Item4 nas propriedades. A saída ilustra o resultado de um conjunto de dados de Tuple<T1,T2,T3,T4> objetos que registram o nome de uma cidade dos EUA, o mês de um ano e a temperatura média alta e baixa para esse mês.

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

Comentários

Este membro é uma implementação do membro de interface explícita. Ele só pode ser usado quando a instância de Tuple<T1,T2,T3,T4> é convertida em uma interface de IStructuralEquatable.

A IEqualityComparer.Equals implementação é chamada somente se other não nullfor , e se puder ser convertida com êxito (em C#) ou convertida (em Visual Basic) em um Tuple<T1,T2,T3,T4> objeto cujos componentes são dos mesmos tipos que a instância atual. Primeiro IStructuralEquatable.Equals(Object, IEqualityComparer) , o método passa os Item1 valores dos Tuple<T1,T2,T3,T4> objetos a serem comparados à IEqualityComparer.Equals implementação. Se essa chamada de método retornar true, o método será chamado novamente e passará os Item2 valores dos dois Tuple<T1,T2,T3,T4> objetos. Se essa chamada de método retornar true novamente, o método será chamado pela terceira vez e passa os Item3 valores dos dois Tuple<T1,T2,T3,T4> objetos. Se essa chamada de método retornar true novamente, o método será chamado pela quarta e última vez e passa os Item4 valores dos dois Tuple<T1,T2,T3,T4> objetos.

Aplica-se a