如何在C#中复制列表 [英] How to copy List in C#
本文介绍了如何在C#中复制列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想复制对象列表,但是我一直在对象之间获取引用。
I want to copy a List of Objects, but i keep getting references between the objects.
List<MyClass> copy = original;
当我更改副本列表中的值时,原始列表也会被修改。
是否有解决方法?
When i change the value in the copy List, the original List ends up modified also. Is there a way to work around it?
推荐答案
您可以执行以下操作:
List<MyClass> copy = original.ToList();
这将对列表进行逐元素复制,而不是复制对列出自己。
This would make an element-by-element copy of the list, rather than copying the reference to the list itself.
这篇关于如何在C#中复制列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文