简体   繁体   English

如何在C#中复制列表

[英]How to copy List in 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? 有办法解决吗?

You can do this: 你可以这样做:

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. 这将对列表进行逐元素复制,而不是将引用复制到列表本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM