c# – 从列表中删除最近添加的项目

c# – 从列表中删除最近添加的项目,第1张

概述List <Customer> collCustList = new List<Customer>(); 我试过了 if(A==B) collCustList.Add(new Customer(99, "H", "P"));else collCustList.Remove(new Customer(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();    }}
总结

以上是内存溢出为你收集整理的c# – 从列表中删除最近添加的项目全部内容,希望文章能够帮你解决c# – 从列表中删除最近添加的项目所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1222983.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存