C# 从列表中删除最近添加的项

C# 从列表中删除最近添加的项,c#,list,C#,List,但它不起作用 如何删除我刚刚添加的新项目(新客户(99,“H”,“p”) 谢谢您正在尝试删除一个新的客户实例,而不是删除刚才添加的客户实例。您需要获取对第一个客户实例的引用并删除该引用,例如: if(A==B) collCustList.Add(new Customer(99, "H", "P")); else collCustList.Remove(new Customer(99, "H", "P")); 或者,更简洁地说,如果您知道要删除最近的客户,您可以

但它不起作用

如何删除我刚刚添加的
新项目(新客户(99,“H”,“p”)


谢谢

您正在尝试删除一个新的客户实例,而不是删除刚才添加的客户实例。您需要获取对第一个客户实例的引用并删除该引用,例如:

if(A==B)
        collCustList.Add(new Customer(99, "H", "P"));

else
        collCustList.Remove(new Customer(99, "H", "P"));
或者,更简洁地说,如果您知道要删除最近的客户,您可以执行以下操作:

Customer customer = new Customer(99, "H", "P");
collCustList.Add(customer);
collCustList.Remove(customer);
如果您没有对要删除的客户实例的现有引用,可以使用如下Linq查询:

collCustList.Remove(collCustList.Last());
甚至只需使用RemoveAll()方法:


这是因为它正在删除添加的同一对象。您的代码正在尝试删除另一个与您刚才添加的对象不同的新对象(它是另一个对象)。

如果您希望这样做,可以使用
List
并让
Customer
实现
IEquatable
。简单的例子:

Customer c = new Customer(99, "H", "P");
List collCustList = new List(); collCustList.Add(c);
collCustList.Remove(c);
使用系统;
使用System.Collections.Generic;
类别客户:IEquatable
{
公共国际一级;
公共字符串c1、c2;
公共客户(int i、字符串c1、字符串c2)
{
这个。i=i;
这1.c1=c1;
这1.c2=c2;
}
布尔系统.等式.等于(客户o)
{
如果(o==null)
返回false;
返回这个。i==o.i&&
这是0.c1==0.c1&&
这是0.c2==0.c2;
}
公共覆盖布尔等于(对象o)
{
返回o!=null&&
this.GetType()==o.GetType()&&
这等于((客户)o);
}
公共覆盖int GetHashCode()
{
return i.GetHashCode()^
c1.GetHashCode()^
c2.GetHashCode();
}
}

如果要使用
Remove()
,则
Customer
类需要实现new Customer()将创建一个新实例,该实例与添加到列表中的实例不同。因此,列表将无法匹配要删除的对象

因此,请使用Robert建议的方法,或者您必须在列表中迭代匹配数据(99,“H”,“p”),然后使用匹配的客户对象引用删除

/jay

如果你想移除

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。抱歉,我在最初的帖子中没有说清楚。谢谢,山姆。我在构建Customer=collCustList时遇到问题。请选择(c=>c.Number==99&&c.Type==H/*etc*/).FirstOrDefault();我想念什么?Thankstorry——我不知道您的客户属性是基于发布的代码的,所以我使用了一些示例。“c.xxx”属性应替换为任何客户类属性名称。谢谢,我确实替换了属性。生成错误是无法将类型“bool”隐式转换为CustomerArrgh-我一直混淆这些错误!请尝试collCustList.Where()而不是:)。(更新后的帖子)
collCustList.RemoveAll(c => c.Number == 99 && c.Type == "H" /* etc */);
Customer c = new Customer(99, "H", "P");
List collCustList = new List(); collCustList.Add(c);
collCustList.Remove(c);
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();
    }
}
  private void removeButton_Click( object sender, EventArgs e )
      {
        if ( displayListBox.SelectedIndex != -1 )
          displayListBox.Items.RemoveAt( displayListBox.SelectedIndex );
      }