简体   繁体   English

在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

I am trying to copy a list to another list using AddRange or other ways but nothing seems to work correctly. 我正在尝试使用AddRange或其他方式将列表复制到另一个列表,但是似乎没有任何正常工作。 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. 如果我使用foreach循环进行复制,它是唯一可行的方法。

    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";
    }

Is there no shorter way to do it? 没有更短的方法吗?

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. 您可以具有两个或多个列表-每个List<Employee>都是一个List<Employee> -并且如果将项目复制到另一个列表或数组,则两个列表中的项目都是相同的项目。

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";

In this example there is one and only one instance of Employee , but four references to that one Employee . 在此示例中,只有一个Employee实例,但是有四个引用指向该Employee

  • x is one reference x是一个参考
  • list[0] is another list[0]是另一个
  • array[0]
  • y

However you refer to that one instance, including list[0].ID = "new id" it's all the same instance of Employee . 但是,您指的是一个实例,包括list[0].ID = "new id"它都是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});
}

The items added to newList aren't the same objects in sourceList . 添加到newList的项目不是sourceList的相同对象。 They are new objects with the same properties. 它们是具有相同属性的新对象。

If you foresee the need to do this frequently then you could make Employee implement ICloneable . 如果您认为需要经常执行此操作,则可以使Employee实现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();
    }
}

Then you can create a cloned list from any IEnumerable set of employees like this: 然后,您可以像这样从IEnumerable任何雇员集中创建一个克隆列表:

var myClonedList = oldList.ToClonedList();

(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.) (吹毛求疵,你没有实现ICloneable 。你可以只写一个方法是复制的对象。 ICloneable仅仅是一个约定,让别人知道他们可以调用.Clone()并创建一个克隆的对象。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建字典副本 <int,List<int> &gt;而不影响原始(C#)? - How do I create a copy of a Dictionary<int,List<int>> without affecting the original (C#)? 如何在C#中过滤包含其他列表的列表? - How do I filter a list containing another list in C#? 从一个列表复制/编辑到另一个列表而不影响原始列表 C# - Copy/Edit from one list to another list without affecting the Original List C# 如何在C#中将一个列表复制到另一个列表 - How to copy one list to another in C# 如何使用c#中的Comparsion将List &lt;&gt;复制到另一个List &lt;&gt; - How to copy a List<> to another List<> with Comparsion in c# 我在列表中使用插入还是C#有其他方法 - Do I use insert in a list or is there another way C# 对“复制”列表所做的更改反映了原始列表 - C# - Changes made on "copy" list are reflecting original list - c# 如何在 while 循环中对克隆的临时列表进行排序而不对原始列表 (C#) 进行排序? - How do I sort a cloned temp list in a while loop without also sorting the original list(C#)? C#.NET WinForms如何从一个列表复制类的实例 <class> 到另一个列表 <class> 使用剪贴板 - C# .NET WinForms How do I copy instance of class from one List<class> to another List<class> using Clipboard 如何将一个列表复制到另一个具有 object 和 C# 附加属性的列表? (没有foreach) - How do I copy one list to another that has an object with additional properties in C# ? (without a foreach)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM