在C#中克隆列表 [英] cloning a list in C#
本文介绍了在C#中克隆列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何在C#中克隆通用列表?
Possible Duplicate:
How do I clone a generic list in C#?
嘿,我一直在尝试克隆列表,到目前为止,我发现了该功能 addRange,但我很确定它不会克隆列表中的对象,而是对列表进行浅表复制,我想知道如何克隆列表 预先感谢.
hey i have been trying to clone a list and so far i found the function addRange but i am pretty sure it does not clone the objects inside the list but doing a shallow copy of the list i would like to know how to clone the list thanks in advance.
推荐答案
要克隆列表,必须克隆每个单独的项目.如果存在针对item类的Clone()的有用实现,则这是使用LINQ的单行代码:
To clone a list, each individual item has to be cloned. Provided a useful implementation of Clone() for the item class exists, this is a one-liner using LINQ:
List<MyType> lstCloned = lstOriginal.Select(i => i.Clone()).ToList();
这篇关于在C#中克隆列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文