- void Txtname2(object li)//带多个参数的方法
- {
- List<string> list = li as List<string>;
- //as的用法,用来把object转换为list集合
- //等价于
- //(List<string>)li强制转换
- //as转换不成功返回null,as比强制转换更简洁高效
- if (list == null)
- {
- foreach (string s in list)
- {
- MessageBox.Show(s);
- }
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Thread xc2 = new Thread(Txtname2); //新建线程
- xc2.IsBackground = true; //后台运行
- xc2.Start(new List<string>() { "aaa", "bbb", "bbb" });
- //匿名实例化,new是用来实例化的,sc2.Start(Objiect obj),传入的
- //参数是object对象.
- //xc2.Start(new List<string>() { "aaa", "bbb", "bbb" });
- //等价于
- //List<string> li = new List<string>() { "aaa", "bbb", "bbb" };
- //xc2.Start(li);
- //如果不写new,只是定义,没有实例化成为一个对象,这是基础
- }
复制代码 |