When to use BlockingCollection and when ConcurrentBag instead of List<T>? When to use BlockingCollection and when ConcurrentBag instead of List<T>? wpf wpf

When to use BlockingCollection and when ConcurrentBag instead of List<T>?


You can indeed use a BlockingCollection, but there is absolutely no point in doing so.

First off, note that BlockingCollection is a wrapper around a collection that implements IProducerConsumerCollection<T>. Any type that implements that interface can be used as the underlying storage:

When you create a BlockingCollection<T> object, you can specify not only the bounded capacity but also the type of collection to use. For example, you could specify a ConcurrentQueue<T> object for first in, first out (FIFO) behavior, or a ConcurrentStack<T> object for last in,first out (LIFO) behavior. You can use any collection class that implements the IProducerConsumerCollection<T> interface. The default collection type for BlockingCollection<T> is ConcurrentQueue<T>.

This includes ConcurrentBag<T>, which means you can have a blocking concurrent bag. So what's the difference between a plain IProducerConsumerCollection<T> and a blocking collection? The documentation of BlockingCollection says (emphasis mine):

BlockingCollection<T> is used as a wrapper for an IProducerConsumerCollection<T> instance, allowing removal attempts from the collection to block until data is available to be removed. Similarly, a BlockingCollection<T> can be created to enforce an upper-bound on the number of data elements allowed in the IProducerConsumerCollection<T> [...]

Since in the linked question there is no need to do either of these things, using BlockingCollection simply adds a layer of functionality that goes unused.


  • List<T> is a collection designed to use in single threadapplications.

  • ConcurrentBag<T> is a class of Collections.Concurrent namespace designedto simplify using collections in multi-thread environments. If youuse ConcurrentCollection you will not have to lock yourcollection to prevent corruption by other threads. You can insertor take data from your collection with no need to write special locking codes.

  • BlockingCollection<T> is designed to get rid of the requirement of checking if new data is available in the shared collection between threads. if there is new data inserted into the shared collection then your consumer thread will awake immediately. So you do not have to check if new data is available for consumer thread in certain time intervals typically in a while loop.


Yes, you could use BlockingCollection for that. finishedProxies would be defined as:

BlockingCollection<string> finishedProxies = new BlockingCollection<string>();

and to add an item, you would write:

finishedProxies.Add(checkResult);

And when it's done, you could create a list from the contents.