Tuple<T1>.Equals(Object) Method

Definition

Returns a value that indicates whether the current Tuple<T1> object is equal to a specified object.

C#
public override bool Equals(object obj);
C#
public override bool Equals(object? obj);

Parameters

obj
Object

The object to compare with this instance.

Returns

true if the current instance is equal to the specified object; otherwise, false.

Examples

The following example calls the Tuple<T1>.Equals(Object) method to compare a Tuple<T1> object whose component is a Double value with three Tuple<T1> objects whose components have the following characteristics:

  • Same type (Double) and same value.

  • Same type (Double), but different value.

  • Different type (Single), but same value.

C#
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

Remarks

The obj parameter is considered to be equal to the current instance under the following conditions:

  • It is a Tuple<T1> object.

  • Its single component is of the same type as the current instance.

  • Its single component is equal to that of the current instance. Equality is determined by the default object equality comparer for each component.

Applies to

Proizvod Verzije
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0