Compartir a través de


Tuple<T1, T2>.Equals Method

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

Returns a value that indicates whether the current Tuple<T1, T2> 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, T2> object.

  • Its two components are of the same types as the current instance.

  • Its two components have the same values as those of the current instance.

Examples

The following example calls the Tuple<T1, T2>.Equals(Object) method to determine whether any of the objects in an array of Tuple<T1, T2> objects are equal to one another. The output reflects the fact that the Equals(Object) method returns true when comparing Tuple<T1, T2> objects whose components have equal values.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim scores() As Tuple(Of String, Nullable(Of Integer)) = _ 
                      { New Tuple(Of String, Nullable(Of Integer))("Dan", 90), _
                        New Tuple(Of String, Nullable(Of Integer))("Ernie", Nothing), _
                        New Tuple(Of String, Nullable(Of Integer))("Jill", 88), _      
                        New Tuple(Of String, Nullable(Of Integer))("Ernie", Nothing), _ 
                        New Tuple(Of String, Nullable(Of Integer))("Nancy", 88), _ 
                        New Tuple(Of String, Nullable(Of Integer))("Dan", 90) }

      ' Compare the Tuple objects
      For ctr As Integer = 0 To scores.Length - 1
         Dim currentTuple As Tuple(Of String, Nullable(Of Integer)) = scores(ctr)
         For innerCtr As Integer = ctr + 1 To scores.Length - 1
            outputBlock.Text += String.Format("{0} = {1}: {2}", _
                              scores(ctr), scores(innerCtr), _ 
                              scores(ctr).Equals(scores(innerCtr))) + vbCrLf
         Next
         outputBlock.Text &= vbCrLf
      Next
   End Sub
End Module
' The example displays the following output:
'       (Dan, 90) = (Ernie, ): False
'       (Dan, 90) = (Jill, 88): False
'       (Dan, 90) = (Ernie, ): False
'       (Dan, 90) = (Nancy, 88): False
'       (Dan, 90) = (Dan, 90): True
'       
'       (Ernie, ) = (Jill, 88): False
'       (Ernie, ) = (Ernie, ): True
'       (Ernie, ) = (Nancy, 88): False
'       (Ernie, ) = (Dan, 90): False
'       
'       (Jill, 88) = (Ernie, ): False
'       (Jill, 88) = (Nancy, 88): False
'       (Jill, 88) = (Dan, 90): False
'       
'       (Ernie, ) = (Nancy, 88): False
'       (Ernie, ) = (Dan, 90): False
'       
'       (Nancy, 88) = (Dan, 90): False
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Tuple<string, Nullable<int>>[] scores = 
                      { new Tuple<string, Nullable<int>>("Dan", 90),
                        new Tuple<string, Nullable<int>>("Ernie", null),
                        new Tuple<string, Nullable<int>>("Jill", 88),
                        new Tuple<string, Nullable<int>>("Ernie", null), 
                        new Tuple<string, Nullable<int>>("Nancy", 88), 
                        new Tuple<string, Nullable<int>>("Dan", 90) };

      // Compare the Tuple objects
      for (int ctr = 0; ctr < scores.Length; ctr++)
      {
         Tuple<string, Nullable<int>> currentTuple = scores[ctr];
         for (int innerCtr = ctr + 1; innerCtr < scores.Length; innerCtr++)
         {
            outputBlock.Text += String.Format("{0} = {1}: {2}",
                              scores[ctr], scores[innerCtr],
                              scores[ctr].Equals(scores[innerCtr])) + "\n";
         }
         outputBlock.Text += "\n";
      }
   }
}
// The example displays the following output:
//       (Dan, 90) = (Ernie, ): False
//       (Dan, 90) = (Jill, 88): False
//       (Dan, 90) = (Ernie, ): False
//       (Dan, 90) = (Nancy, 88): False
//       (Dan, 90) = (Dan, 90): True
//       
//       (Ernie, ) = (Jill, 88): False
//       (Ernie, ) = (Ernie, ): True
//       (Ernie, ) = (Nancy, 88): False
//       (Ernie, ) = (Dan, 90): False
//       
//       (Jill, 88) = (Ernie, ): False
//       (Jill, 88) = (Nancy, 88): False
//       (Jill, 88) = (Dan, 90): False
//       
//       (Ernie, ) = (Nancy, 88): False
//       (Ernie, ) = (Dan, 90): False
//       
//       (Nancy, 88) = (Dan, 90): 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.