c# – 从列表中删除最近添加的项目
发布时间:2020-12-16 00:13:33 所属栏目:百科 来源:网络整理
导读:List Customer collCustList = new ListCustomer(); 我试过了 if(A==B) collCustList.Add(new Customer(99,"H","P"));else collCustList.Remove(new Customer(99,"P")); 但它不起作用 我怎么能删除刚刚添加的新项目(新客户(99,“H”,“P”))? 谢谢 解决方法
List <Customer> collCustList = new List<Customer>(); 我试过了 if(A==B) collCustList.Add(new Customer(99,"H","P")); else collCustList.Remove(new Customer(99,"P")); 但它不起作用 我怎么能删除刚刚添加的新项目(新客户(99,“H”,“P”))? 谢谢 解决方法
如果您希望这个工作,您可以使用List< T>并让客户实现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(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |