积极答复者
c#把一个List<>赋值给另一个list<>

问题
-
c#怎么把一个List<> A赋值给另一个list<> B,当B改变时不影响A里面的值
demo:
public class People { public string Name { get; set; } public int Age { get; set; } } class Program { static void Main(string[] args) { List<People> list1 = new List<People> { new People{ Age=12, Name="张三" } }; List<People> list2 = new List<People>(); list1.ForEach(a => list2.Add(a)); list2[0].Name = "王五"; foreach (People model in list1) { Console.WriteLine(model.Name); } foreach (People model in list2) { Console.WriteLine(model.Name); } Console.ReadLine(); } }
please verify my account
- 已编辑 lctk 2016年12月27日 3:44
答案
-
Hi,
其实不是ForEach的原因,是A和B指向的people的对象是一样的,list的地址是不一样的,但是list里面的people还是指向同一块地址。
如果你用下面的代码
foreach (var item in list1) { //list2.Add(new People() { Age = item.Age, Name = item.Name}); list2.Add(item); }
如果你使用隐藏的方法就不会有问题,因为people对象也被改变也是不一样的。 Item其实拿的是一个引用,而不是值传递。
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已编辑 Hart WangModerator 2016年12月27日 7:09
- 已标记为答案 lctk 2016年12月27日 23:56
- 取消答案标记 lctk 2016年12月28日 0:24
- 已标记为答案 lctk 2016年12月30日 5:42
全部回复
-
Hello lctk,
根据你的代码,我进行测试了,输入的结果是一样的,也就是你改变B的结果,是会影响到A的结果。A和B都是指向同一块内存区域。
我建议你使用那个链接的第二种方法。
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
-
Hi,
其实不是ForEach的原因,是A和B指向的people的对象是一样的,list的地址是不一样的,但是list里面的people还是指向同一块地址。
如果你用下面的代码
foreach (var item in list1) { //list2.Add(new People() { Age = item.Age, Name = item.Name}); list2.Add(item); }
如果你使用隐藏的方法就不会有问题,因为people对象也被改变也是不一样的。 Item其实拿的是一个引用,而不是值传递。
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已编辑 Hart WangModerator 2016年12月27日 7:09
- 已标记为答案 lctk 2016年12月27日 23:56
- 取消答案标记 lctk 2016年12月28日 0:24
- 已标记为答案 lctk 2016年12月30日 5:42
-
Hi,
你可以参考下面的代码:
list1.ForEach(a => list2.Add(new People() { Age = a.Age, Name = a.Name }));
没有简单的写法,你只要注意我们要修改 people的值就可以解决问题。
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
-
目前我没有想到好的方法,我觉得赋值必须要一个个赋值。
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.