我如何克隆在C#泛型列表? [英] How do I clone a generic list in C#?
本文介绍了我如何克隆在C#泛型列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C#对象的泛型列表,并希望克隆名单。在列表中的项目是可复制的,但似乎没有成为一种选择做 list.Clone()
。
I have a generic list of objects in C#, and wish to clone the list. The items within the list are cloneable, but there doesn't seem to be an option to do list.Clone()
.
有没有解决这个简单的方法?
Is there an easy way around this?
推荐答案
您可以使用扩展方法。
static class Extensions
{
public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
{
return listToClone.Select(item => (T)item.Clone()).ToList();
}
}
这篇关于我如何克隆在C#泛型列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文