I need a thread-safe collection to hold items without duplicates. ConcurrentBag<T> allows non-unique items and HashSet<T> is not thread-safe. Is there a collection like this in .NET Framework 4.5?
I need a thread-safe collection to hold items without duplicates. ConcurrentBag<T> allows non-unique items
Share
I suggest you use a
ConcurrentDictionaryand just use a dummy value for each entry. It’s annoying in terms of efficiency (having all those dummy values) but I suspect in the majority of applications that would be insignificant.You may well want to wrap this in your own
ConcurrentSetimplementation, which just does enough for your purposes, so that you don’t need to see the abstraction leakiness in much of your code.