How to get the remaining elements of the Tuple in C#?


To get the remaining elements of the Tuple, the Rest property is used. The code is as follows −

Example

Live Demo

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);
   }
}

Output

This will produce the following output −

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)

Example

Let us now see another example −

 Live Demo

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);
   }
}

Output

This will produce the following output −

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))

Updated on: 10-Dec-2019

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements