Thread-Safe collections in C#


The .NET Framework 4 introduced System.Collections.Concurrent namespace. The namespace has numerous collection classes. These classes are both thread-safe and scalable. Multiple threads can safely add or remove items from these collections,

The following concurrent collection types use lightweight synchronization mechanisms: SpinLock, SpinWait, etc. These are new in .NET Framework 4.

Let us see the concurrent collection in C# −

Sr.No. Type & Description
1

BlockingCollection<T>

Bounding and blocking functionality for any type.

2

ConcurrentDictionary<TKey,TValue>

Thread-safe implementation of a dictionary of key-value pairs.

3

ConcurrentQueue<T>

Thread-safe implementation of a FIFO (first-in, first-out) queue.

4

ConcurrentStack<T>

Thread-safe implementation of a LIFO (last-in, first-out) stack.

5

ConcurrentBag<T>

Thread-safe implementation of an unordered collection of elements.

6

IProducerConsumerCollection<T>

The interface that a type must implement to be used in a BlockingCollection

Updated on: 30-Jul-2019

616 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements