Tuple<T1,T2,T3,T4>.IStructuralEquatable.Equals 方法

定義

傳回值,這個值表示依據指定的比較方法,目前的 Tuple<T1,T2,T3,T4> 物件是否等於指定的物件。

 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

參數

other
Object

與這個執行個體相互比較的物件。

comparer
IEqualityComparer

物件,定義要用來評估這兩個物件是否相等的方法。

傳回

Boolean

如果目前的執行個體和指定的物件相等,則為 true,否則為 false

實作

範例

下列範例會定義實 Item3And4Comparer 作 介面的類別, IEqualityComparer 並變更評估物件是否相等的方式 Tuple<T1,T2,T3,T4> 。 方法一律會在 true 傳遞 Item1Tuple<T1,T2,T3,T4> 個 物件的 和 Item2 屬性值時傳回 ,並呼叫 obj.Equals 方法來評估其 Item3 屬性值。 此方法呼叫會傳回 true,也會呼叫 obj.Equals 方法來評估 Tuple 的 Item4 屬性值。 因此,方法只會根據 和 Item4 屬性的值 Item3 來測試是否相等。 輸出說明記錄美國城市名稱、一年月份,以及該月份平均高與低溫度的物件資料集 Tuple<T1,T2,T3,T4> 結果。

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

備註

這個成員是明確介面成員實作, 只有在 Tuple<T1,T2,T3,T4> 執行個體轉換成 IStructuralEquatable 介面時,才能使用這個成員。

只有在 不是 nullother ,才會呼叫 實 IEqualityComparer.Equals 作,如果它可以在 C#) 中成功轉換 ( (Tuple<T1,T2,T3,T4> ,或Visual Basic) 轉換成元件與目前實例類型相同的物件, 方法 IStructuralEquatable.Equals(Object, IEqualityComparer) 會先傳遞 Item1 要與實作比較 IEqualityComparer.Equals 的物件 Tuple<T1,T2,T3,T4> 值。 如果這個方法呼叫傳 true 回 ,則會再次呼叫 方法,並傳遞 Item2Tuple<T1,T2,T3,T4> 個 物件的值。 如果這個方法呼叫再次傳 true 回,則會第三次呼叫 方法,並傳遞 Item3Tuple<T1,T2,T3,T4> 個 物件的值。 如果這個方法呼叫再次傳 true 回,則會針對第四次和最後一次呼叫 方法,並傳遞 Item4Tuple<T1,T2,T3,T4> 個 物件的值。

適用於