Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

จะรับองค์ประกอบที่เหลือของ Tuple ใน C # ได้อย่างไร


เพื่อให้ได้องค์ประกอบที่เหลือของ Tuple จะใช้คุณสมบัติ 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))