首页 > 解决方案 > List.remove() 在 C# 中不起作用

问题描述

我正在尝试从列表中删除客户,如下所示。

customerList.Remove(customer);

我在运行之前和之后显示了整个列表,它表明没有任何改变。我在这里查看了另一篇文章:C# 中的“List.Remove”不删除项目?但我无法辨别这个解决方案是如何应用于我的。

我知道我需要直接从列表中获取对象,而不是它的副本。我只是不确定这是不是同样的情况,如果是这样,我将不胜感激!

string file = @"C:~\bin\Debug\Customer.xml";
List<Customer> customerList = new List<Customer> { };
XmlSerializer customerSerializer = new XmlSerializer(customerList.GetType());
using (StreamReader customerStreamReader = new StreamReader(file))
{
    customerList = (List<Customer>)customerSerializer.Deserialize(customerStreamReader);
}
Console.WriteLine();
customerList.Remove(customer);

标签: c#list

解决方案


推荐阅读