在C#如何复制列表到另一个列表,在修改复制的列表,原始列表不修改的方式 [英] In C# How do I copy a list to another list in such a way that on modifying the copied list the original list is not modified
问题描述
我试图使用AddRange或其他方式将列表复制到另一个列表,但似乎没有正确工作。无论我尝试如何,我修改复制的列表,它修改原始列表。如果我使用foreach循环复制它的唯一方式。
I am trying to copy a list to another list using AddRange or other ways but nothing seems to work correctly. No matter whatever way I try if, I modify the copied list it modifies the original list too. The only way it work if I copy using foreach loop.
static void Main(string[] args)
{
var test = new List<Employee>{ new Employee { ID = "101", Name ="ABC1"}, new Employee{ID = "102", Name = "ABC2"}, new Employee{ ID = "103", Name = "ABC3"}};
var tets2 = new List<Employee>(test);
tets2[0].ID = "1-73";
TestMethod(test.ToList());
}
private static void TestMethod(List<Employee> test)
{
var test1 = new List<Employee>(test);
test1.AddRange(test);
//This is the only one that work
foreach(var item in test)
{
var x = new Employee { ID = item.ID, Name = item.Name };
test1.Add(x);
}
test1[0].ID = "104";
}
有没有更短的方法?
推荐答案
它不修改列表 - 它正在修改列表中的项目。
您可以有两个或多个列表 - 每个都是 List< Employee>
- 如果将项目复制到另一个列表或数组,是相同的项目。
It's not modifying the list - it's modifying the item in the list.
You can have two or more lists - each is a List<Employee>
- and if you copy the items to another list or array, the items in both lists are the same items.
var x = new Employee { ID = "xyz", Name = "Bob" };
var list = new List<Employee>();
list.Add(x);
var array = new Employee[]{x};
var y = x;
y.ID = "abc";
在此示例中,只有一个实例 Employee
,但有四个引用到 Employee
。
In this example there is one and only one instance of Employee
, but four references to that one Employee
.
-
x
是一个参考 -
是另一个
-
数组[0]
code>
x
is one referencelist[0]
is anotherarray[0]
y
但是,你引用一个实例,包括 list [0] .ID = new id
它与 Employee
的所有相同的实例。
However you refer to that one instance, including list[0].ID = "new id"
it's all the same instance of Employee
.
要创建一个副本,你可以这样做 - 这是一个详细的例子,不一定是你想实现它的方式:
To create a copy you could do something like this - this is a verbose example, not necessarily the way you'd want to actually implement it:
var newList = new List<Employee>();
foreach(var employee in sourceList) //the list you want to copy from
{
newList.Add(new Employee{ID=employee.ID, Name=employee.Name});
}
添加到 newList
在 sourceList
中不是相同的对象。它们是具有相同属性的新对象。
The items added to newList
aren't the same objects in sourceList
. They are new objects with the same properties.
如果你预见需要频繁这样做,那么你可以使 Employee
实现 ICloneable
。
If you foresee the need to do this frequently then you could make Employee
implement ICloneable
.
public class Employee : ICloneable
{
public string ID {get;set;}
public string Name {get;set;}
public object Clone()
{
return new Employee{ID=ID, Name=Name};
}
}
或者,因为属性只是值类型
Or, because the properties are just value types, you could just do this, which copies properties from the source object into a new object.
public class Employee : ICloneable
{
public string ID {get;set;}
public string Name {get;set;}
public object Clone()
{
return this.MemberwiseClone();
}
}
然后要创建一个新列表,方法:
Then to create a new list you could create an extension method:
public static class EmployeeExtensions
{
public static List<Employee> ToClonedList(this IEnumerable<Employee> source)
{
return source.Select(employee => employee.Clone() as Employee).ToList();
}
}
然后,您可以从任何 IEnumerable
这样的员工集:
Then you can create a cloned list from any IEnumerable
set of employees like this:
var myClonedList = oldList.ToClonedList();
(要拆分毛发,您不必实现 ICloneable
只是写一个复制对象的方法。
ICloneable
只是一个约定,让其他人知道他们可以调用 .Clone ()
并创建一个克隆的对象。)
(To split hairs, you don't have to implement ICloneable
. You could just write a method that copies the object. ICloneable
is just a convention that lets others know they can call .Clone()
and create a cloned object.)
这篇关于在C#如何复制列表到另一个列表,在修改复制的列表,原始列表不修改的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!