C# 中的嵌套元组
我们首先声明一个嵌套元组。
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); // 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); } }
输出
100 200 300 400 500 600 Nested Tuple... 720 750 780 8th element... 800
广告