c#编程指南(四) 组元(Tuple)
c#编程指南(四) 组元(Tuple)
组元是C# 4.0引入的一个新特性,编写的时候需要基于.NET Framework 4.0或者更高版本。组元使用泛型来简化一个类的定义。
先以下面的一段代码为例子:
1 public class Point
2 {
3 public int X { get; set; }
4 public int Y { get; set; }
5 }
6
7
8 //the user customer data type.
9 Point p = new Point() { X = 10, Y = 20 };
10 //use the predefine generic tuple type.
11 Tuple<int, int> p2 = new Tuple<int, int>(10, 20);
12
13 //
14 Console.WriteLine(p.X + p.Y);
15 Console.WriteLine(p2.Item1 + p2.Item2);
2 {
3 public int X { get; set; }
4 public int Y { get; set; }
5 }
6
7
8 //the user customer data type.
9 Point p = new Point() { X = 10, Y = 20 };
10 //use the predefine generic tuple type.
11 Tuple<int, int> p2 = new Tuple<int, int>(10, 20);
12
13 //
14 Console.WriteLine(p.X + p.Y);
15 Console.WriteLine(p2.Item1 + p2.Item2);
一个简单的包含两个Int类型成员的类,传统的方法定义point需要写很多代码,但是使用tuple却只有一句,组元多用于方法的返回值。如果一个函数返回多个类型,这样就不在用out , ref等输出参数了,可以直接定义一个tuple类型就可以了。非常方便。
下面的列子稍微复杂一点:
1 //1 member
2 Tuple<int> test = new Tuple<int>(1);
3 //2 member ( 1< n <8 )
4 Tuple<int, int> test2 = Tuple.Create<int, int>(1,2);
5 //8 member , the last member must be tuple type.
6 Tuple<int, int, int, int, int, int, int, Tuple<int>> test3 = new Tuple<int, int, int, int, int, int, int, Tuple<int>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int>(8));
7
8 //
9 Console.WriteLine(test.Item1);
10 Console.WriteLine(test2.Item1 + test2.Item2);
11 Console.WriteLine(test3.Item1 + test3.Item2 + test3.Item3 + test3.Item4 + test3.Item5 + test3.Item6 + test3.Item7 + test3.Rest.Item1);
2 Tuple<int> test = new Tuple<int>(1);
3 //2 member ( 1< n <8 )
4 Tuple<int, int> test2 = Tuple.Create<int, int>(1,2);
5 //8 member , the last member must be tuple type.
6 Tuple<int, int, int, int, int, int, int, Tuple<int>> test3 = new Tuple<int, int, int, int, int, int, int, Tuple<int>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int>(8));
7
8 //
9 Console.WriteLine(test.Item1);
10 Console.WriteLine(test2.Item1 + test2.Item2);
11 Console.WriteLine(test3.Item1 + test3.Item2 + test3.Item3 + test3.Item4 + test3.Item5 + test3.Item6 + test3.Item7 + test3.Rest.Item1);
第一个定义包含一个成员。
第二个定义包含两个成员,并且使用create方法初始化。
第三个定义展示了tuple最多支持8个成员,如果多于8个就需要进行嵌套。注意第8个成员很特殊,如果有8个成员,第8个必须嵌套定义tuple。如果上面所示。
下面又举了两个嵌套定义的例子,简单的要命:
1 //2 member ,the second type is the nest type tuple.
2 Tuple<int, Tuple<int>> test4 = new Tuple<int, Tuple<int>>(1, new Tuple<int>(2));
3 //10 member datatype. nest the 8 parameter type.
4 Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>> test5 = 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));
5
6
7 //
8 Console.WriteLine(test4.Item1 + test4.Item2.Item1);
9 Console.WriteLine(test5.Item1 + test5.Item2 + test5.Item3 + test5.Item4 + test5.Item5 + test5.Item6 + test5.Item7 + test5.Rest.Item1 + test5.Rest.Item2 + test5.Rest.Item3);
2 Tuple<int, Tuple<int>> test4 = new Tuple<int, Tuple<int>>(1, new Tuple<int>(2));
3 //10 member datatype. nest the 8 parameter type.
4 Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>> test5 = 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));
5
6
7 //
8 Console.WriteLine(test4.Item1 + test4.Item2.Item1);
9 Console.WriteLine(test5.Item1 + test5.Item2 + test5.Item3 + test5.Item4 + test5.Item5 + test5.Item6 + test5.Item7 + test5.Rest.Item1 + test5.Rest.Item2 + test5.Rest.Item3);
示例代码:下载