Переопределить Equals
и GetHashCode
как:
class Element
{
public int id;
protected bool Equals(Element other)
{
return id == other.id;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Element) obj);
}
public override int GetHashCode()
{
return id; //or id.GetHashCode();
}
//..... rest of the class
См.: List
Этот метод определяет равенство по используя сопоставитель равенства по умолчанию, как определено реализацией объектом метода
IEquatable
для T (тип значений в списке)..Equals