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

定義

現在の Tuple<T1> オブジェクトが、指定されたオブジェクトと等しいかどうかを示す値を返します。

public:
 override bool Equals(System::Object ^ obj);
public override bool Equals (object obj);
public override bool Equals (object? obj);
override this.Equals : obj -> bool
Public Overrides Function Equals (obj As Object) As Boolean

パラメーター

obj
Object

このインスタンスと比較するオブジェクト。

戻り値

Boolean

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

次の例では、メソッドをTuple<T1>.Equals(Object)呼び出して、コンポーネントが値であるオブジェクトと、Double次の特性を持つコンポーネントを持つ 3 つのTuple<T1>オブジェクトを比較Tuple<T1>します。

  • 同じ型 (Double) と同じ値。

  • 同じ型 (Double)、ただし値は異なります。

  • 異なる型 (Single)、ただし同じ値。

using System;

public class Example
{
   public static void Main()
   {
      var doubleTuple1 = Tuple.Create(12.3455);
      var doubleTuple2 = Tuple.Create(16.8912);
      var doubleTuple3 = Tuple.Create(12.3455);
      var singleTuple1 = Tuple.Create(12.3455f);
      var tuple2 = Tuple.Create("James", 97.3); 
        
      // 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);
      // Compare first tuple with a Tuple(Of Single) with the same value.
      TestEquality(doubleTuple1, singleTuple1);
      // Compare a 1-tuple with a 2-tuple.
      TestEquality(doubleTuple1, tuple2); 
   }

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

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

let doubleTuple1 = Tuple.Create 12.3455
let doubleTuple2 = Tuple.Create 16.8912
let doubleTuple3 = Tuple.Create 12.3455
let singleTuple1 = Tuple.Create 12.3455f
let tuple2 = Tuple.Create("James", 97.3) 

// 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
// Compare first tuple with a Tuple(Of Single) with the same value.
testEquality doubleTuple1 singleTuple1
// Compare a 1-tuple with a 2-tuple.
testEquality doubleTuple1 tuple2 

// The example displays the following output:
//       (12.3455) = (16.8912): False
//       (12.3455) = (12.3455): True
//       (12.3455) = (12.3455): False
//       (12.3455) = (James, 97.3): False
Module Example
    Sub Main()
        Dim doubleTuple1 = Tuple.Create(12.3455)

        Dim doubleTuple2 = Tuple.Create(16.8912)
        Dim doubleTuple3 = Tuple.Create(12.3455)
        Dim singleTuple1 = Tuple.Create(CSng(12.3455))
        Dim tuple2 = Tuple.Create("James", 97.3) 
        ' 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)
        ' Compare first tuple with a Tuple(Of Single) with the same value.
        TestEquality(doubleTuple1, singleTuple1)
        ' Compare a 1-tuple with a 2-tuple.
        TestEquality(doubleTuple1, tuple2) 
    End Sub
    
   Private Sub TestEquality(tuple As Tuple(Of Double), obj As Object)
      Try
         Console.WriteLine("{0} = {1}: {2}", tuple.ToString(),
                                            obj.ToString,
                                            tuple.Equals(obj))
      
      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.3455): True
'       (12.3455) = (12.3455): False
'       (12.3455) = (James, 97.3): False

注釈

この obj パラメーターは、次の条件下で現在のインスタンスと等しいと見なされます。

  • オブジェクトです Tuple<T1>

  • その 1 つのコンポーネントは、現在のインスタンスと同じ型です。

  • その 1 つのコンポーネントは、現在のインスタンスのコンポーネントと同じです。 等しいかどうかは、各コンポーネントの既定のオブジェクトの等値比較子によって判断されます。

適用対象