Tuple
Tuple是C# 4.0时出的新特性,.Net Framework 4.0以上版本可用。
默认情况.Net Framework元组仅支持1到7个元组元素,如果有8个元素或者更多,需要使用Tuple的嵌套和Rest属性去实现。另外Tuple类提供创造元组对象的静态方法。
class Program { static void Main(string[] args) { var testTuple1 = new Tuple<int, int, int, int, int, int>(1, 2, 3, 4, 5, 6); Console.WriteLine($"Item 1: {testTuple1.Item1}, Item 6: {testTuple1.Item6}"); var testTuple2 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int, int, int>(8, 9, 10)); Console.WriteLine($"Item 1: {testTuple2.Item1}, Item 10: {testTuple2.Rest.Item3}"); var testTuple3 = Tuple.Create<int, int, int, int, int, int>(1, 2, 3, 4, 5, 6); Console.WriteLine($"Item 1: {testTuple3.Item1}, Item 6: {testTuple3.Item6}"); var testTuple4 = Tuple.Create<int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8); Console.WriteLine($"Item 1: {testTuple4.Item1}, Item 8: {testTuple4.Rest.Item1}"); Console.ReadLine(); } }