【发布时间】:2017-01-09 00:26:39
【问题描述】:
我有一个 C# 中的通用对象列表,并希望克隆该列表。
List<Student> listStudent1 = new List<Student>();
List<Student> listStudent2 = new List<Student>();
下面我用了一个扩展方法,但是不行: (当 listStudent2 发生变化时 -> 影响 listStudent1)
public static List<T> CopyList<T>(this List<T> oldList)
{
var newList = new List<T>(oldList.Capacity);
newList.AddRange(oldList);
return newList;
}
我想继续在 listStudent2 中添加元素或进行更改而不影响 listStudent1。 我该怎么做?
【问题讨论】: