如何在 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屋!
查看全文