我有一个类对象列表,并希望删除一个项目,但它不起作用:
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);
}
Run Code Online (Sandbox Code Playgroud)
RemoveAt(1)工作并删除ID为1的项目.
我认为删除(A)应该删除值为"A"的项目.但这不起作用.有人可以解释原因吗?什么是按值删除的正确方法?