共用方式為


Tuple<T1>.Equals Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Overrides Function Equals ( _
    obj As Object _
) As Boolean
public override bool Equals(
    Object obj
)

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.

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 has the same value as that of the current instance.

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.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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(outputBlock, doubleTuple1, doubleTuple2)
      ' Compare first tuple with a Tuple(Of Double) with the same value.
      TestEquality(outputBlock, doubleTuple1, doubleTuple3)
      ' Compare first tuple with a Tuple(Of Single) with the same value.
      TestEquality(outputBlock, doubleTuple1, singleTuple1)
      ' Compare a 1-tuple with a 2-tuple.
      TestEquality(outputBlock, doubleTuple1, tuple2)
   End Sub

   Private Sub TestEquality(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal tuple As Tuple(Of Double), ByVal obj As Object)
      Try
         outputBlock.Text += String.Format("{0} = {1}: {2}", tuple.ToString(), _
                                            obj.ToString, _
                                            tuple.Equals(obj)) + vbCrLf

      Catch e As ArgumentException
         If obj.GetType.IsGenericType Then
            If obj.GetType().Name = "Tuple`1" Then
               outputBlock.Text += String.Format("Cannot compare a Tuple(Of {0}) with a Tuple(Of {1}).", _ 
                              tuple.Item1.GetType().Name, obj.Item1.GetType().Name) & vbCrLf
            Else
               outputBlock.Text += String.Format("Cannot compare a {0} with a {1}.", tuple.GetType().Name, _ 
                                                                     obj.GetType().Name) & vbCrLf
            End If
         Else
            outputBlock.Text += String.Format("Cannot compare a {0} with a {1}.", tuple.GetType().Name, _
                                                                  obj.GetType().Name) & vbCrLf
         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
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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(outputBlock, doubleTuple1, doubleTuple2);
      // Compare first tuple with a Tuple(Of Double) with the same value.
      TestEquality(outputBlock, doubleTuple1, doubleTuple3);
      // Compare first tuple with a Tuple(Of Single) with the same value.
      TestEquality(outputBlock, doubleTuple1, singleTuple1);
      // Compare a 1-tuple with a 2-tuple.
      TestEquality(outputBlock, doubleTuple1, tuple2);
   }

   private static void TestEquality(System.Windows.Controls.TextBlock outputBlock, Tuple<double> tuple, object obj)
   {
      outputBlock.Text += String.Format("{0} = {1}: {2}", tuple.ToString(),
                                          obj.ToString(),
                                          tuple.Equals(obj)) + "\n";
   }
}
// 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

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference