次の方法で共有


Tuple<T1>.IStructuralEquatable.Equals(Object, IEqualityComparer) メソッド

定義

指定された比較メソッドに基づいて、現在の Tuple<T1> オブジェクトが指定したオブジェクトと等しいかどうかを示す値を返します。

 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

2 つのオブジェクトが等しいかどうかの評価に使用するメソッドを定義するオブジェクト。

戻り値

現在のインスタンスが指定したオブジェクトと等しい場合は true。それ以外の場合は false

実装

次の例では、2 つの浮動小数点値が互いにほぼ等しい場合 (つまり、一方の値が他方の値の .01% 以内にある場合) に等しいと見なす実装を定義 IEqualityComparer します。

using System;
using System.Collections;

public class Tuple1Comparer : IEqualityComparer
{
   new public bool Equals(object x, object y)
   {
      // Check if x is a floating point type. If x is, then y is.
      if (x is double | x is float)
      {   
         // Convert to Double values.
         double dblX = (double) x;
         double dblY = (double) y;
         if (Double.IsNaN(dblX) | Double.IsInfinity(dblX) |
             Double.IsNaN(dblY) | Double.IsInfinity(dblY)) 
            return dblX.Equals(dblY);   
         else
            return Math.Abs(dblX - dblY) <= dblX * .0001;
      }
      else
      {
         return x.Equals(y);
      }
   }
   
   public int GetHashCode(object obj)
   {
      return obj.GetHashCode();
   }
}

public class Example
{
   public static void Main()
   {
      var doubleTuple1 = Tuple.Create(12.3455);

      var doubleTuple2 = Tuple.Create(16.8912);
      var doubleTuple3 = Tuple.Create(12.3449599);

      // Compare first tuple with a Tuple<double> with a different value.
      TestEquality(doubleTuple1, doubleTuple2);
      //Compare first tuple with a Tuple<double> with the same value.
      TestEquality(doubleTuple1, doubleTuple3);
   }

   private static void TestEquality(Tuple<double> tuple, object obj)
   {
      Console.WriteLine("{0} = {1}: {2}", tuple.ToString(),
                                             obj.ToString(),
                                             ((IStructuralEquatable)tuple).Equals(obj, new Tuple1Comparer()));
   }
}
// The example displays the following output:
//       (12.3455) = (16.8912): False
//       (12.3455) = (12.3449599): True
open System
open System.Collections

type Tuple1Comparer() =
    interface IEqualityComparer with
        member _.Equals(x: obj, y: obj) =
            match x with
            | :? double as dblX -> 
                // Convert to Double values.
                let dblY = y :?> double
                if Double.IsNaN dblX || Double.IsInfinity dblX ||
                    Double.IsNaN dblY || Double.IsInfinity dblY then
                    dblX.Equals dblY
                else
                    abs (dblX - dblY) <= dblX * 0.0001
            | _ ->
                x.Equals y
                
        member _.GetHashCode(obj: obj) =
            obj.GetHashCode()

let testEquality (tuple: Tuple<double>) (obj: obj) =
    printfn $"{tuple} = {obj}: {(tuple :> IStructuralEquatable).Equals(obj, Tuple1Comparer())}"

let doubleTuple1 = Tuple.Create 12.3455
let doubleTuple2 = Tuple.Create 16.8912
let doubleTuple3 = Tuple.Create 12.3449599

// Compare first tuple with a Tuple<double> with a different value.
testEquality doubleTuple1 doubleTuple2
//Compare first tuple with a Tuple<double> with the same value.
testEquality doubleTuple1 doubleTuple3

// The example displays the following output:
//       (12.3455) = (16.8912): False
//       (12.3455) = (12.3449599): True
Imports System.Collections

Public Class Tuple1Comparer : Implements IEqualityComparer
   Public Overloads Function Equals(x As Object, y As Object) As Boolean _
                   Implements IEqualityComparer.Equals
      ' Check if x is a floating point type. If x is, then y is.
      If TypeOf x Is Double Or TypeOf x Is Single Then   
         ' Convert to Double values.
         Dim dblX As Double = CDbl(x)
         Dim dblY As Double = CDbl(y)
         If Double.IsNan(dblX) Or Double.IsInfinity(dblX) Or _
            Double.IsNan(dblY) Or Double.IsInfinity(dblY) Then
            Return dblX.Equals(dblY)   
         Else
            Return Math.Abs(dblX - dblY) <= dblY * .0001
         End If
      Else
         Return x.Equals(y)
      End If                    
   End Function
   
   Public Overloads Function GetHashCode(obj As Object) As Integer _
                   Implements IEqualityComparer.GetHashCode
      Return obj.GetHashCode()
   End Function
End Class

Module Example
   Public Sub Main()
      Dim doubleTuple1 = Tuple.Create(12.3455)

      Dim doubleTuple2 = Tuple.Create(16.8912)
      Dim doubleTuple3 = Tuple.Create(12.3449599)

      ' Compare first tuple with a Tuple(Of Double) with a different value.
      TestEquality(doubleTuple1, doubleTuple2)
      ' Compare first tuple with a Tuple(Of Double) with the same value.
      TestEquality(doubleTuple1, doubleTuple3)
   End Sub

   Private Sub TestEquality(tuple As Tuple(Of Double), obj As Object)
      Try
         Console.WriteLine("{0} = {1}: {2}", tuple.ToString(),
                                             obj.ToString,
                                             DirectCAst(tuple, IStructuralEquatable).Equals(obj, New Tuple1Comparer()))
      
      Catch e As ArgumentException
         If obj.GetType.IsGenericType Then 
            If obj.GetType().Name = "Tuple`1" Then 
               Console.WriteLine("Cannot compare a Tuple(Of {0}) with a Tuple(Of {1}).", 
                              tuple.Item1.GetType().Name, obj.Item1.GetType().Name)
            Else
               Console.WriteLine("Cannot compare a {0} with a {1}.", tuple.GetType().Name, 
                                                                     obj.GetType().Name)
            End If
         Else
            Console.WriteLine("Cannot compare a {0} with a {1}.", tuple.GetType().Name,
                                                                  obj.GetType().Name)
         End If
      End Try
   End Sub
End Module
' The example displays the following output:
'       (12.3455) = (16.8912): False
'       (12.3455) = (12.3449599): True

注釈

このメンバーは、明示的なインターフェイス メンバーの実装です。 これは、Tuple<T1> のインスタンスが IStructuralEquatable インターフェイスにキャストされる場合のみ、使用できます。

実装はIEqualityComparer.Equals、 が でないnull場合にのみ呼び出され、(C#では) 正常にキャストできる場合、または (Visual Basic では) 現在のインスタンスと同じ型の単一コンポーネントを持つオブジェクトにTuple<T1>変換できる場合otherにのみ呼び出されます。 メソッドには、現在の Item1 インスタンスのコンポーネントと Item1 、 パラメーターで表される オブジェクトの Tuple<T1> コンポーネントが other 渡されます。

適用対象

こちらもご覧ください