Tuple(Of T1).Equals Method (Object)

 

Returns a value that indicates whether the current Tuple(Of T1) object is equal to a specified object.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Overrides Function Equals (
	obj As Object
) As Boolean

Parameters

obj
Type: System.Object

The object to compare with this instance.

Return Value

Type: System.Boolean

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

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

  • It is a Tuple(Of 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.

The following example calls the Tuple(Of T1).Equals(Object) method to compare a Tuple(Of T1) object whose component is a Double value with three Tuple(Of 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.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: