让我们首先声明一个嵌套元组。
var tuple = Tuple.Create(100, 200, 300, 400, 500, 600, Tuple.Create(720, 750, 780),800 );
上面,我们使用Tuple.Create添加了一个嵌套的元组。
现在要在嵌套元组中显示元素,请嵌套Item属性。元组中正弦的第7个项目是嵌套的,我们将使用以下内容来获取嵌套的项目-
tuple.Item7.Item1; tuple.Item7.Item2; tuple.Item7.Item3;
让我们看完整的代码。
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); //嵌套元组 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); } }
输出结果
100 200 300 400 500 600 Nested Tuple... 720 750 780 8th element... 800