C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#でタプルの残りの要素を取得するにはどうすればよいですか?


タプルの残りの要素を取得するには、Restプロパティを使用します。コードは次のとおりです-

using System;
public class Demo {
   public static void Main(String[] args){
      var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, 2000);
      var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, 2000);
      Console.WriteLine("Is Tuple1 equal to Tuple2? = "+tuple1.Equals(tuple2));
      Console.WriteLine("HashCode of Tuple1 = "+tuple1.GetHashCode());
      Console.WriteLine("HashCode of Tuple2 = "+tuple2.GetHashCode());
      Console.WriteLine("Tuple1 Item 1st = "+tuple1.Item1);
      Console.WriteLine("Tuple2 Item 1st = "+tuple2.Item1);
      Console.WriteLine("Tuple1 Item 2nd = "+tuple1.Item2);
      Console.WriteLine("Tuple2 Item 2nd = "+tuple2.Item2);
      Console.WriteLine("Tuple1 Item 4th = "+tuple1.Item4);
      Console.WriteLine("Tuple2 Item 4th = "+tuple2.Item4);
      Console.WriteLine("Tuple1 Item 5th = "+tuple1.Item5);
      Console.WriteLine("Tuple2 Item 5th = "+tuple2.Item5);
      Console.WriteLine("Tuple1 Item 6th = "+tuple1.Item6);
      Console.WriteLine("Tuple2 Item 6th = "+tuple2.Item6);
      Console.WriteLine("Tuple1 Item 7th = "+tuple1.Item7);
      Console.WriteLine("Tuple2 Item 7th = "+tuple2.Item7);
      Console.WriteLine("Tuple1 rest value = "+tuple1.Rest);
      Console.WriteLine("Tuple2 rest value = "+tuple2.Rest);
   }
}

出力

これにより、次の出力が生成されます-

Is Tuple1 equal to Tuple2? = True HashCode of Tuple1 = 3247155
HashCode of Tuple2 = 3247155
Tuple1 Item 1st = 75
Tuple2 Item 1st = 75
Tuple1 Item 2nd = 200
Tuple2 Item 2nd = 200
Tuple1 Item 4th = 700
Tuple2 Item 4th = 700
Tuple1 Item 5th = 100
Tuple2 Item 5th = 100
Tuple1 Item 6th = 1200
Tuple2 Item 6th = 1200
Tuple1 Item 7th = 1500
Tuple2 Item 7th = 1500
Tuple1 rest value = (2000)
Tuple2 rest value = (2000)

別の例を見てみましょう-

using System;
public class Demo {
   public static void Main(String[] args){
      var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, Tuple.Create("AB", 2000, "CD"));
      var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, Tuple.Create(2500, 3500, 4000, "XY"));
      Console.WriteLine("Is Tuple1 equal to Tuple2? = "+tuple1.Equals(tuple2));
      Console.WriteLine("HashCode of Tuple1 = "+tuple1.GetHashCode());
      Console.WriteLine("HashCode of Tuple2 = "+tuple2.GetHashCode());
      Console.WriteLine("Tuple1 Item 1st = "+tuple1.Item1);
      Console.WriteLine("Tuple2 Item 1st = "+tuple2.Item1);
      Console.WriteLine("Tuple1 Item 2nd = "+tuple1.Item2);
      Console.WriteLine("Tuple2 Item 2nd = "+tuple2.Item2);
      Console.WriteLine("Tuple1 Item 4th = "+tuple1.Item4);
      Console.WriteLine("Tuple2 Item 4th = "+tuple2.Item4);
      Console.WriteLine("Tuple1 Item 5th = "+tuple1.Item5);
      Console.WriteLine("Tuple2 Item 5th = "+tuple2.Item5);
      Console.WriteLine("Tuple1 Item 6th = "+tuple1.Item6);
      Console.WriteLine("Tuple2 Item 6th = "+tuple2.Item6);
      Console.WriteLine("Tuple1 Item 7th = "+tuple1.Item7);
      Console.WriteLine("Tuple2 Item 7th = "+tuple2.Item7);
      Console.WriteLine("Tuple1 rest value = "+tuple1.Rest);
      Console.WriteLine("Tuple2 rest value = "+tuple2.Rest);
   }
}

出力

これにより、次の出力が生成されます-

Is Tuple1 equal to Tuple2? = False
HashCode of Tuple1 = -1121878415
HashCode of Tuple2 = -835095725
Tuple1 Item 1st = 75
Tuple2 Item 1st = 75
Tuple1 Item 2nd = 200
Tuple2 Item 2nd = 200
Tuple1 Item 4th = 700
Tuple2 Item 4th = 700
Tuple1 Item 5th = 100
Tuple2 Item 5th = 100
Tuple1 Item 6th = 1200
Tuple2 Item 6th = 1200
Tuple1 Item 7th = 1500
Tuple2 Item 7th = 1500
Tuple1 rest value = ((AB, 2000, CD)) Tuple2 rest value = ((2500, 3500, 4000, XY))

  1. Tkinter.Listboxのアイテムのインデックスを取得するにはどうすればよいですか?

    Tkinterリストボックスウィジェットを使用して、アイテムのリストを作成します。リストボックス内の各アイテムには、垂直方向に順番に割り当てられるいくつかのインデックスがあります。 リストボックスでクリックされたアイテムのインデックスを取得したいとします。次に、最初に list.curselection()を使用してアイテムの現在の選択をキャプチャするボタンを作成する必要があります メソッドを実行してから、 get()を使用してインデックスを出力します。 メソッド。 例 # Import the required libraries from tkinter import * # Crea

  2. Pythonでネストされたタプルで一意の要素を取得する方法

    ネストされたタプルで一意の要素を取得する必要がある場合は、ネストされたループと「set」演算子を使用できます。 Pythonには、「set」と呼ばれるデータ型が付属しています。この「セット」には、一意の要素のみが含まれています。 このセットは、共通部分、差、和集合、対称差などの操作を実行するのに役立ちます。 以下は同じのデモンストレーションです- 例 my_list_1 = [(7, 8, 0), (0 ,3, 45), (3, 2, 22), (45, 12, 9)] print ("The list of tuple is : " ) print(my_list