标签:
1)
先说组元:一个数据结构,由通过逗号分割的,用于传递给一个程序或者操作系统的一系列值的组合。
NET Framework 直接支持一至七元素的元组
//一个元素的元组 Tuple<int> test = new Tuple<int>(34); //两个元素的元组 1<n<8 Tuple<string, int> test2 = Tuple.Create<string, int>("str", 2); Tuple<int, int> test2_1 = new Tuple<int, int>(2,2); //8个元素的元组(注意,Tuple<类型...>: 基本"类型"最多7个, 第八个元素类型必须也为元组) 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)); //也可以这样 Tuple<int, int, Tuple<int, int>> test_i_i_Tii = new Tuple<int, int, Tuple<int, int>>(1,1,new Tuple<int,int>(2,3)); Console.WriteLine(test.Item1); Console.WriteLine(test2.Item1 + test2.Item2); Console.WriteLine(test2_1.Item1 + test2_1.Item2); Console.WriteLine(test3.Item1 + test3.Item2 + test3.Item3 + test3.Item4 + test3.Item5 + test3.Item6 + test3.Item7 + test3.Rest.Item1);结果:
namespace TupleDemo { class Program { static void Main(string[] args) { //使用out拿到多个返回值 string outparam = ""; int returnvalue = FunOutParamDemo(out outparam); Console.WriteLine(returnvalue + " " + outparam); //使用元组拿到多个返回值 Tuple<int, string> r = FunTupleParamDemo(); Console.WriteLine(r.Item1 + " " + r.Item2); Console.Read(); } /// <summary> /// out关键字,实现返回两个返回值 /// </summary> /// <param name="o"></param> /// <returns></returns> public static int FunOutParamDemo(out string o) { o = "returnValue"; return 10; } /// <summary> /// 使用元组实现【间接】返回【两个】返回值 /// </summary> /// <returns></returns> public static Tuple<int, string> FunTupleParamDemo() { return new Tuple<int, string>(10, "returnValue"); } } }运行结果:
标签:
原文地址:http://blog.csdn.net/zhifeiya/article/details/45227563