共用方式為


Tuple<T1,T2>.Equals(Object) 方法

定義

傳回值,這個值表示目前的 Tuple<T1,T2> 物件是否等於指定的物件。

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,T2>.Equals(Object) 呼叫 方法,以判斷 物件陣列 Tuple<T1,T2> 中的任何物件是否彼此相等。 輸出會反映方法在比較 Tuple<T1,T2> 元件具有相等值的物件時所傳 true 回的事實 Equals(Object)

using System;

public class Example
{
   public static void Main()
   {
      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++)
      {
         for (int innerCtr = ctr + 1; innerCtr < scores.Length; innerCtr++)
         {
            Console.WriteLine("{0} = {1}: {2}", 
                              scores[ctr], scores[innerCtr], 
                              scores[ctr].Equals(scores[innerCtr]));
         }
         Console.WriteLine();
      }
   }
}
// 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
open System

let scores = 
    [| Tuple<string, Nullable<int>>("Dan", 90)
       Tuple<string, Nullable<int>>("Ernie", Nullable())
       Tuple<string, Nullable<int>>("Jill", 88)
       Tuple<string, Nullable<int>>("Ernie", Nullable()) 
       Tuple<string, Nullable<int>>("Nancy", 88)
       Tuple<string, Nullable<int>>("Dan", 90) |]

// Compare the Tuple objects
for ctr = 0 to scores.Length - 1 do
    for innerCtr = ctr + 1 to scores.Length - 1 do
        printfn $"{scores[ctr]} = {scores[innerCtr]}: {scores[ctr].Equals scores[innerCtr]}"
    printfn ""
// 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
Module Example
   Public Sub Main()
      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
         For innerCtr As Integer = ctr + 1 To scores.Length - 1
            Console.WriteLine("{0} = {1}: {2}", 
                              scores(ctr), scores(innerCtr), 
                              scores(ctr).Equals(scores(innerCtr)))
         Next
         Console.WriteLine()
      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

備註

參數 obj 在下列情況下會被視為等於目前的實例:

  • Tuple<T1,T2>它是 物件。

  • 其兩個元件的類型與目前的實例相同。

  • 它的兩個元件等於目前實例的元件。 是否相等由每個元件的預設物件相等比較子決定。

適用於