List <Customer> collCustList = new List<Customer>();
我试过了
if(A==B)
collCustList.Add(new Customer(99, "H", "P"));
else
collCustList.Remove(new Customer(99, "H", "P"));
但它不起作用
如何删除我刚刚添加的
new item(new Customer(99, "H", "P"))
?谢谢
最佳答案
如果你想让它工作,你可以使用 List<T>
并让 Customer
实现 IEquatable<Customer>
。简单的例子:
using System;
using System.Collections.Generic;
class Customer : IEquatable<Customer>
{
public int i;
public string c1, c2;
public Customer(int i, string c1, string c2)
{
this.i = i;
this.c1 = c1;
this.c2 = c2;
}
bool System.IEquatable<Customer>.Equals(Customer o)
{
if(o == null)
return false;
return this.i == o.i &&
this.c1 == o.c1 &&
this.c2 == o.c2;
}
public override bool Equals(Object o)
{
return o != null &&
this.GetType() == o.GetType() &&
this.Equals((Customer) o);
}
public override int GetHashCode()
{
return i.GetHashCode() ^
c1.GetHashCode() ^
c2.GetHashCode();
}
}
关于c# - 从列表中删除最近添加的项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2742313/