元组简单示例
数组合并了相同类型的对象,而元组合并了不同类型的对象。
泛型元组的使用
public static Tuple<int, int> Divide(int dividend, int divisor)
{
int result = dividend / divisor;
int reminder = dividend % divisor;
return Tuple.Create<int, int>(result, reminder);
}
//调用
var result = Divide(5, 2);
Console.WriteLine("result of divison:{0},reminder:{1}",result.Item1,result.Item2);
超过8个元组的使用方法
由于最后一个模板参数是TResult,表示必须给它传递一个元组。这样就可以创建带任意个参数的元组了。
//定义方法如下
Tuple<int, int, int, int, int, int,int,Tuple<Tuple<bool, bool>>> tup = Tuple.Create<int, int, int, int, int, int, int, Tuple<bool, bool>>(1, 2, 3, 4, 5, 6, 7, Tuple.Create<bool, bool>(true, true));
var tup2 = Tuple.Create<int, int, int, int, int, int, int, Tuple<bool, bool>>(1, 2, 3, 4, 5, 6, 7, Tuple.Create<bool, bool>(true, true));
//定义方法的写法
public static Tuple<int, int, int, int, int, int, int, Tuple<Tuple<bool, bool>>> DividePlus(int i1, int i2, int i3, int i4, int i5, int i6, int i7, Tuple<bool, bool> result)
{
return Tuple.Create<int, int, int, int, int, int, int, Tuple<bool, bool>>(i1, i2, i3, i4, i5, i6, i7, Tuple.Create<bool, bool>(result.Item1, result.Item2));
}