roman m
roman m

Reputation: 26561

How do i make sure that List<T>.Contains(T) works with my custom class?

I use .net 2.0 (C#)

I have a Person class, and i pupulate List< Person> p from the database.

i know for a fact that my list has duplicates, but when i try to remove the duplicates they don't get removed.

Does my Person class need to implement any interfaces for List< T>.Contains(T) to work properly?

Any other ideas?

Thank you.

Upvotes: 2

Views: 1398

Answers (5)

BFree
BFree

Reputation: 103770

Your Person class should implement IEquatable

Upvotes: 8

redjackwong
redjackwong

Reputation: 1568

You should override Equals and GetHashCode method.

Upvotes: 4

Trevor
Trevor

Reputation: 729

You need to overload Object.Equals(Object obj) in your class.

Upvotes: 1

Andrew Hare
Andrew Hare

Reputation: 351688

The example you reference is not a solution for removing dupes from a list, it is a function that takes a list and yields an iterator that will exclude duplicates. If you need to dedupe the entire list in one go you would need to take the IEnumerable<T> returned from the function and pass it into a new List<T>.

Upvotes: 1

Logan Capaldo
Logan Capaldo

Reputation: 40346

The docs say "This method determines equality using the default equality comparer EqualityComparer(T).Default".

Upvotes: 0

Related Questions