I need a concurrent collection that doesn't allow duplicates (to use in BlockingCollection
as Producer/Consumer).
I don't need strict order of elements.
From another hand i want to minimize the maximum time of element "live" in collection. I.e. collection mustn't be LIFO, ideally it should be FIFO.
Well I would say that I need ConcurrentQueue
with no duplicates allowed, but ConcurrentBag
with no duplicates also might work.
Why C# doesn't contain anything like that and probably someone already created it?
This question is result of my previous question What type of IProducerConsumerCollection<T> to use for my task?
There are no built-in .Net libraries that combine this set of rules for a collection. You have three options:
The last two aren't very efficient (one with memory, the other with CPU, I/O, locking) and are messier because of the need for explicit locking, but would accomplish the task. They will be quicker to implement, but if the trade-offs don't meet your requirements you'll have to go with option #1.
Well if you strictly want to have no duplicates, you need 'Sets'. For instance NHibernate uses Iesi.Collections to provide such functionality. Taking Iesi, you can build your own functionality around the provided 'Set' classes (DictionarySet, HashSet, SortedSet). Source: http://www.codeproject.com/KB/recipes/sets.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With