111,106
社区成员




//第一种
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());
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;}
}