一:在.net4.0之前,我们需要一个方法返回多个参数,通常用以下几种方式:
1、全局变量
2、out 、 ref 变量
3、定义类或结构体,把需要反回参数封装成属性
二:.net4.0新增的Tuple类,可以方便的解决这个问题:
Tuple类,它代表一个有序的N元组。所谓元组,其实就是“数值对”,比如以下就是一个4元组:(1,2.1,"stringTest",new object()),可以调用Tuple.Create静态方法或使用new 关键字直接创建一个Tuple对象。 .net 类库中定义了拥有1-7个泛型参数的泛型Tuple.使用Tuple对象作为方法返回值,可以很容易地包含多个结果.
例:
var tupleOne = new Tuple<int, string>(1, "Hello World");
Console.WriteLine("Tuple contains: " + tupleOne.Item1 + " and " + tupleOne.Item2);
var tupleTwo = Tuple.Create("Hello World", 2010);
var tupleThree = Tuple.Create("Hello World", 2010, new SomeClass());
Console.WriteLine("Tuple contains: " + tupleThree.Item1 + ", " + tupleThree.Item2 + " and " + tupleThree.Item3.MyProperty);