简体   繁体   English

C#list.remove(item)从我想要的其他列表中删除项目

[英]C# list.remove(item) removes item from other list I want

I have following code: 我有以下代码:

List<T> firstList; //list with some items
List<T> secondList = firstList //copyOfFirstList


List<T> elementsToDelete = secondList.FindAll(whichShouldIDelete);


foreach(var item in elementsToDelete){
     secondList.Remove(item);
}

The problem is that Remove() method does not delete items only from secondList. 问题在于,Remove()方法不会仅从secondList删除项目。 Items are deleted from both firstList and secondList. 项目将从firstList和secondList中删除。 So when I remove 20 items from secondList, the same elements are removed from firstList. 因此,当我从secondList中删除20个项目时,相同的元素也将从firstList中删除。 What may be the cause of this situation? 这种情况可能是什么原因?

The problem is with this line: 问题在于此行:

List<T> secondList = firstList //copyOfFirstList

It is not creating a copy of the list, instead there are now two references pointing to the same instance of list. 它不是在创建列表的副本,而是现在有两个引用指向列表的同一实例

You can do: 你可以做:

List<T> secondList = firstList.ToList(); // explicitly creating a new list. 

or 要么

List<T> secondList = new List<T>(firstList);

But remember, if your List<T> has a reference type (class) then the object of each list would point to the same instance. 但是请记住,如果您的List<T>具有引用类型(类),则每个列表的对象将指向同一实例。 (A shallow copy of object will be available). (对象的浅表副本将可用)。 So if you do: 因此,如果您这样做:

firstList[0].SomeProperty = 2;

and if that item exists in secondList then you will see the property change in second list as well. 如果该项目存在于secondList那么您还将在第二个列表中看到属性更改。

Your assumption: 您的假设:

List<T> secondList = firstList //copyOfFirstList

is only partially right. 仅部分正确。 Is it a shallow reference copy. 它是浅表参考副本。

The secondList refers to the same object in a different variable. secondList在不同的变量中引用相同的对象。

Google for DeepCopy List to find answers that may match your expectation Google for DeepCopy列表可查找可能符合您期望的答案

As List<T> is reference type, not value one, then List<T> secondList = firstList; 由于List<T>是引用类型,而不是值1,因此List<T> secondList = firstList; is not a copy, but just a reference to the same object. 不是副本,而只是对同一对象的引用。

It is because both lists point to the same "address", so editing one affects the other. 这是因为两个列表都指向相同的“地址”,所以编辑其中一个会影响另一个。

try 尝试

List<T> firstList = new List<T>();
List<T> secondList = new List<T>();
secondList.AddRange(firstList);
public static List<T> CloneList<T>(List<T> oldList)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream();
        formatter.Serialize(stream, oldList);
        stream.Position = 0;
        return (List<T>)formatter.Deserialize(stream);
    } 

    private void button1_Click(object sender, EventArgs e)
    {
        List<string> firstList = new List<string>() { "1", "2", "3"}; //list with some items
        List<string> secondList = CloneList(firstList); //copyOfFirstList

        List<string> elementsToDelete = new List<string>() { "1" }; // do some logic to find all items you need to delete

        foreach (var item in elementsToDelete)
        {
            secondList.Remove(item);
        }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM