Quantcast
Viewing latest article 2
Browse Latest Browse All 8

Answer by Bob Heck for C# Set collection?

I know this is an old thread, but I was running into the same problem and found HashSet to be very unreliable because given the same seed, GetHashCode() returned different codes. So, I thought, why not just use a List and hide the add method like this

public class UniqueList<T> : List<T>{    public new void Add(T obj)    {        if(!Contains(obj))        {            base.Add(obj);        }    }}

Because List uses the Equals method solely to determine equality, you can define the Equals method on your T type to make sure you get the desired results.


Viewing latest article 2
Browse Latest Browse All 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>