Nested Tuples in C#

ProgrammingServer Side ProgrammingCsharp

Let us first declare a nested tuple.

var tuple = Tuple.Create(100, 200, 300, 400, 500, 600, Tuple.Create(720, 750, 780),800 );

Above, we added a nested tuple using Tuple.Create.

Now to display elements in a nested tuple, nest the Item properties. Sine 7th item in the tuple is nested, we will be using the following to get the nested items −

tuple.Item7.Item1;
tuple.Item7.Item2;
tuple.Item7.Item3;

Let us see the complete code.

Example

 Live Demo

using System;
public class Program {
   public static void Main() {
      var tuple = Tuple.Create(100, 200, 300, 400, 500, 600, Tuple.Create(720, 750, 780),800 );
      Console.WriteLine(tuple.Item1);
      Console.WriteLine(tuple.Item2);
      Console.WriteLine(tuple.Item3);
      Console.WriteLine(tuple.Item4);
      Console.WriteLine(tuple.Item5);
      Console.WriteLine(tuple.Item6);

      // nested tuple
      Console.WriteLine("\nNested Tuple...");
      Console.WriteLine(tuple.Item7.Item1);
      Console.WriteLine(tuple.Item7.Item2);
      Console.WriteLine(tuple.Item7.Item3);
      Console.WriteLine("\n8th element...\n"+tuple.Rest.Item1);
   }
}

Output

100
200
300
400
500
600

Nested Tuple...
720
750
780

8th element...
800
raja
Published on 11-Sep-2018 09:39:12
Advertisements