简体   繁体   English

按值从列表中删除项目

[英]Remove item from list by value

I have a list of class objects and want to remove one item but it doesn´t work: 我有一个类对象列表,想要删除一个项目,但是它不起作用:

    class Person
    {
        public string name; 
        public Person(string s)
        {
            this.name = s;
        }
    }

    void ABC()
    {
        List<Person> newPersonList = new List<Person>();
        newPersonList.Add(new Person("A"));
        newPersonList.Add(new Person("B"));
        newPersonList.Add(new Person("C"));

        newPersonList.Remove(A);
        newPersonList.RemoveAt(1);
    }

RemoveAt(1) works and deletes item with the ID 1. RemoveAt(1)工作并删除ID为1的项目。

I think Remove(A) should delete the item with the value "A". 我认为Remove(A)应该删除值为“ A”的项目。 But this is not working. 但这是行不通的。 Can someone explain why? 有人可以解释为什么吗? And what is the right way to delete by value? 而按值删除的正确方法是什么?

Easiest way to remove from list by element's property value: 按元素的属性值从列表中删除的最简单方法:

newPersonList.RemoveAll(p => p.name == "A");

Nicer way would be to change Person like that: 更好的方法是像这样更改Person

class Person : IEquatable<Person>
{
    public readonly string Name;
    public Person(string name)
    {
        if (string.IsNullOrWhiteSpace(name))
            throw new ArgumentException("name");
        Name = name;
    }
    public static implicit operator string(Person p)
    {
        return p.Name;
    }
    public static implicit operator Person(string name)
    {
        return new Person(name);
    }
    public bool Equals(Person other)
    {
        return Name.Equals(other.Name);
    }
}

And then use it like that: 然后像这样使用它:

var newPersonList = new List<Person>
{
    new Person("A"),
    new Person("B"),
    new Person("C")
};
newPersonList.Remove("A");

Or even like that: 甚至像这样:

var newPersonList = new List<Person> { "A", "B", "C" };
newPersonList.Remove(new Person("A"));

So you want .net magically to guess that by "A" string you mean field name ? 因此,您想让.net神奇地猜测,用"A"字符串表示字段name什么? How is it supposed to derive it? 应该如何推导它?

I would suggest you use a Dictionary if you want to operate things by their key (name in this case): 如果您想通过它们的键(在这种情况下为名称)操作事物,我建议您使用字典:

var dict = new Dictionary<string, Person>() {
   {"A", new Person("A")}
}

//and later

dict.Remove("A");

You have not declared A. To do newPersonList.Remove(A); 您尚未声明A。要做newPersonList.Remove(A);

You have to declare an object Person A and add it to newPersonList 您必须声明一个对象Person A并将其添加到newPersonList

Person A = new Person("A");
newPersonList.Add(A);

Like other said "What is A ?". 就像其他人说的“什么是A ?”。 If it would be a variable that contains a Person that this will work. 如果它将是一个包含Person的变量,那么它将起作用。

void ABC()
{
    var A = new Person("A");
    var B = new Person("B");
    var C = new Person("C");
    List<Person> newPersonList = new List<Person>();
    newPersonList.Add(A);
    newPersonList.Add(B);
    newPersonList.Add(C);

    newPersonList.Remove(A);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM