Thread Safe Concurrent Collection in C#


The .NET framework offers some collection classes specifically used in multithreading. These collections are internally used synchronization hence we can call them thread safe collections. These collections can be accessed by multiple threads at a time hence they are called concurrent collections.

Here is the list of concurrent collections in C# 4.0

  • ConcurrentStack<T>
  • ConcurrentQueue<T>
  • BlockingCollection<T>
  • ConcurrentBag<T>
  • ConcurrentDictionary<TKey,T>

Let’s discuss ConcurrentCollections in detail,

ConcurrentStack<T>: This is thread-safe last-in-first-out collection.

Difference between Stack and Concurrent Stack

Sr. No




Not thread-safe



Stack has Pop method

ConcurrentStaick has TryPop method


Can add single item at a time.

Can add multiple item at a time.


Can remove single item at a time

Can remove multiple item at a time.


Add item using Push method

Add item using or PushRange method


Remove item using Pop method

Remove item using TryPop or TryPopRange method.

Push: Add item at top of ConcurrentStack.

PushRange: Tries to add multiple items at top of ConcurrentStack.

TryPeek: Tries to return last item from ConcurrentStack.

TryPop: Tries to pop and return last item from ConcurrentStack.

ConcurrentQueue<T>: This is thread-safe first-in-first-out collection.

Difference between Queue and ConcurrentQueue

Sr. No




Not thread-safe



Queue has Enqueue and Dequeue methods

ConcurrentQueue has Enqueue and TryDequeue methods


Can add single item at a time.

Can add multiple item at a time.


Can remove single item at a time

Can remove multiple item at a time.

Enqueue: Add item to end of ConcurrentQueue.

TryDequeue: Tries to remove and return first item from ConcurrentQueue.

TryPeek: Return first item from ConcurrentQueue.


This collection is thread-safe for adding and removing data. You can set maximum upper limit to add items in this collection. Removing an item from     the collection can be blocked until item becomes available. 

Add: Add item into BlockingCollection.

Take: Removes item from BlockingCollection .

TryAdd: Tries to add item into BlockingCollection.

TryTake: Tries to remove item from BlockingCollection.


ConcurrentBag is thread-safe collection. This collection is like just at bag or heap of objects. It has not ordering and it allow duplicate items.

ConCurrentBag has mainly three methods.

Add: Add item into ConcurrentBag collection.

TryTake: Tries to remove and return item from ConcurrentBag.

TryPeek: Tries to return item from ConcurrentBag.


Concurrent dictionary is thread-safe collection. It is used to store key-value pair in multi-threaded environment.

Add: Add item into concurrent dictionary.

Remove: Remove item from concurrent dictionary.

Update: Update existing item in concurrent dictionary.

TryUpdate: This method check whether item is exist or not before update.

AddOrUpdate: This method add item into concurrent dictionary if that object does not exist.

GetOrAdd: This method return current value in dictionary if it exist otherwise add as new value.

Read more articles on Multithreading:

Recommended Free Ebook
Similar Articles