元组(Tuple)
1.第一个例子
private Tuple<int, int> Divide(int dividend, int divisor)
{
int result = dividend / divisor;
int reminder = dividend % divisor;
return Tuple.Create<int, int>(result, reminder); //返回两个相同类型元素的元组
}
--------测试-------------
private void button1_Click(object sender, EventArgs e)
{
Tuple<int, int> result = Divide(13, 2);
Console.WriteLine("result of divison:{0}," +
"reminder:{1}", result.Item1, result.Item2); //用属性item1,item2访问元组的项
}
-------结果-------------
result of divison:6,reminder:1
2.第二个例子
private Tuple<int, string> MyTest2(int dividend, string Name)
{
int result = dividend / 2;
string name = "Hello," + Name;
return Tuple.Create<int, string>(result, name); //返回两个不同类型元素的元组
}
--------测试-------------
private void button2_Click(object sender, EventArgs e)
{
Tuple<int, string> result = MyTest2(13, "limin");
Console.WriteLine("result of divison:{0}," +
"Name:{1}", result.Item1, result.Item2); //用属性item1,item2访问元组的项
}