public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { List<Person> list = new List<Person> { new Person() { Name = "dfdf", Age = 50 }, new Person() { Name = "tt", Age = 30 } }; List<Person> list2 = new List<Person>(list); list2[0].Name = "张三"; list.ForEach(x => MessageBox.Show(x.Name)); } } class Person { public string Name; public int Age; }
类似上面那种代码,list2复制了list的元素,当修改list2中的元素的Name属性值时,list中元素也修改了。请问,如何做到list中的元素不受影响呢?Person的属性值少还可以新创建Person对象到list2中去,如果遇到属性值很多,该咋办呢?