Nested Tuples in C#


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("
Nested Tuple...");       Console.WriteLine(tuple.Item7.Item1);       Console.WriteLine(tuple.Item7.Item2);       Console.WriteLine(tuple.Item7.Item3);       Console.WriteLine("
8th element...
"+tuple.Rest.Item1);    } }

Output

100
200
300
400
500
600

Nested Tuple...
720
750
780

8th element...
800

Updated on: 23-Jun-2020

419 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements