-
如果你的方法需要返回多个值 或者 方法需要传递多个值。
-
如果你不想为某些功能专门去定义一个类。
-
如果你有若干个混杂元素,并且想提供对其简单的访问方式
public TupleTest()
{
InitializeComponent();
//演示Tuple
var testTuple6 = new Tuple<int, int, int, int, int, int, int,Tuple<int,int,int,int>>(11, 12, 13, 14, 15, 16, 17,new Tuple<int,int,int,int>(21,22,23,24));
//this.RTB_test.Text=Convert.ToString(testTuple6.Rest.Item4);
//实例一 学生信息:姓名,年龄,体重
var studentInfo = new Tuple<string, int, int>("test",23,120);
this.RTB_test.Text = "学生姓名:" + Convert.ToString(studentInfo.Item1) + "\n"+"学生年龄:"+ studentInfo.Item2+"\n"+"学生体重:"+studentInfo.Item3;
//MessageBox.Show($"Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]");
}
#region 从方法返回多个值
static Tuple<string,int,int> GetStudentInfo(string name)
{
return new Tuple<string, int, int>(name,23,110);
}
private void button1_Click(object sender, EventArgs e)
{
var studentInfo = GetStudentInfo("chen");
this.RTB_test.Text = studentInfo.Item1 + studentInfo.Item2 + studentInfo.Item3;
}
#endregion
#region 用于单参数的多值传递()
private void WriteStudentInfo(object student)
{
var studentInfo = student as Tuple<string, int, int>;
this.RTB_test.Text = studentInfo.Item1 + studentInfo.Item2 + studentInfo.Item3;
}
#endregion
private void button2_Click(object sender, EventArgs e)
{
var t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(WriteStudentInfo));
t.Start(new Tuple<string,int,int>("chen",34,130));
while (t.IsAlive)
{
System.Threading.Thread.Sleep(50);
}
}