怎样list<student> A里面的所有对象赋值给另一个list<student> B,为啥我有时可以直接用等号赋值,有时又必须要foreach循环赋值 怎样list<student> A里面的所有对象赋值给另一个list<student> B,为啥我有时可以直接用等号赋值,有时又必须要foreach循环赋值才能真正取到A中的所有对象?是不是下面两种方法都可以?(有时觉得只能用第二种)1.list<student> B=A; (应该和list<student> B=new list<student>();B=A;一样的?)2.foreach(var item in B)B.ADD(item); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 第一种写法,A 和B指向相同的引用,当A改变时,B也会跟着改变第二种守法,A的心迹不会影响到B给你写了个DEMO,希望你能理解:void Main(){ List<Student> A=new List<Student> { new Student{ Id=1, Name="Tim"}, new Student{ Id=2, Name="SCAUSCNU"} }; List<Student> B=A; List<Student> C=new List<Student>(); A.ForEach(a=>C.Add(a)); Console.WriteLine(A.Count()); //2 Console.WriteLine(B.Count()); //2 Console.WriteLine(C.Count()); //2 A.Add(new Student{ Id=3, Name="csdn"}); Console.WriteLine(); Console.WriteLine(A.Count()); //3 Console.WriteLine(B.Count()); //3 注意此处为何是3 ??? Console.WriteLine(C.Count()); //2 }public class Student{ public int Id{get;set;} public string Name{get;set;}} 第一种写法,A 和B指向相同的引用,当A改变时,B也会跟着改变第二种写法,A的改变不会影响到B 1楼居然是黄色网站,嘎嘎用第二种方法第一种方法是直接把该a赋值给 b,把a的引用地址给了b,这个时候,如果a改变了,b也会跟着改变,因为b的地址就是a.list<student> B=new list<student>();B=A; 和list<student> B=A 是一样的,都是把a的引用地址给了a,楼主可以看看值类型和引用类型相关的知识第二种方法是把a集合中的值给了b,a的改变,和b没有关系了 //第一种 List<string> lsa = new List<string>(); lsa.Add("aa"); lsa.Add("bb"); List<string> lsB = new List<string>(); lsB = lsa; lsa.Clear(); MessageBox.Show(lsB.Count.ToString()); //第二种 List<string> lsa = new List<string>(); lsa.Add("aa"); lsa.Add("bb"); List<string> lsB = new List<string>(); foreach (string var in lsa) { lsB.Add(var); } lsa.Clear(); MessageBox.Show(lsB.Count.ToString()); 关于未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序 Visa救命! 一段小程序 出错 大家帮着看看 求VC#写的操作EXCEL的程序源码 问一个简单的正则表达式 关于URLENCODE的问题 100分CLSID {655E2790-B904-406C-BA83-2B00D7B49A6A} 的 COM 对象无效或未注册。 .net里怎么没有ip地址控件?? 怎样从Main()函数启动 C#的SDI问题,C#的MDI解决方法 同一个解决方案里、C++怎么向C#通信? 请问下大们Handle具体有些什么用?
第二种守法,A的心迹不会影响到B给你写了个DEMO,希望你能理解:
void Main()
{
List<Student> A=new List<Student>
{
new Student{ Id=1, Name="Tim"},
new Student{ Id=2, Name="SCAUSCNU"}
};
List<Student> B=A;
List<Student> C=new List<Student>();
A.ForEach(a=>C.Add(a));
Console.WriteLine(A.Count()); //2
Console.WriteLine(B.Count()); //2
Console.WriteLine(C.Count()); //2
A.Add(new Student{ Id=3, Name="csdn"});
Console.WriteLine();
Console.WriteLine(A.Count()); //3
Console.WriteLine(B.Count()); //3 注意此处为何是3 ???
Console.WriteLine(C.Count()); //2
}
public class Student
{
public int Id{get;set;}
public string Name{get;set;}
}
第二种写法,A的改变不会影响到B
list<student> B=new list<student>();B=A; 和list<student> B=A 是一样的,都是把a的引用地址给了a,楼主可以看看值类型和引用类型相关的知识第二种方法是把a集合中的值给了b,a的改变,和b没有关系了
List<string> lsa = new List<string>();
lsa.Add("aa");
lsa.Add("bb");
List<string> lsB = new List<string>();
lsB = lsa;
lsa.Clear();
MessageBox.Show(lsB.Count.ToString());
//第二种
List<string> lsa = new List<string>();
lsa.Add("aa");
lsa.Add("bb");
List<string> lsB = new List<string>();
foreach (string var in lsa)
{
lsB.Add(var);
}
lsa.Clear();
MessageBox.Show(lsB.Count.ToString());