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.
I think Remove(A) should delete the item with the value "A". But this is not working. Can someone explain why? And what is the right way to delete by value?
Remove(A);
? You haven't declaredA
. In general you need to overrideEquals
inPerson
or/and implementIEquatable<Person>
– Tim Schmelter Aug 13 '15 at 13:55