System.Collections.Concurrent Namespace

Overview

Namespace: System.Collections.Concurrent

Assembly: mscorlib (in mscorlib.dll)

The System.Collections.Concurrent namespace provides several thread-safe collection classes that can be used in multithreaded applications. These collections are optimized for scenarios where multiple threads need to access and modify collections concurrently.

Key benefits include reduced locking overhead and improved performance in concurrent scenarios compared to using traditional synchronization primitives with standard collections.

Key Classes and Interfaces

The System.Collections.Concurrent namespace contains the following primary collection types:

ConcurrentBag<T>

A thread-safe collection that is optimized for scenarios where the collection is accessed by multiple threads, and the order of items is not important. Items can be added and removed efficiently.

Learn More

ConcurrentDictionary<TKey, TValue>

A thread-safe dictionary that provides efficient access and modification from multiple threads. It allows concurrent reads and writes with minimal contention.

Learn More

ConcurrentQueue<T>

A thread-safe, FIFO (First-In, First-Out) collection. It is suitable for producer-consumer scenarios where items are added to the back and removed from the front.

Learn More

ConcurrentStack<T>

A thread-safe, LIFO (Last-In, First-Out) collection. It is useful when you need a stack-like data structure that can be accessed concurrently.

Learn More

BlockingCollection<T>

A thread-safe collection that supports blocking operations. Producers can add items, and consumers can take items. It can block when the collection is full or empty.

Learn More

Partitioner<TSource>

An abstract class that defines a static class for creating partitioners, which are used to divide a collection into partitions for parallel processing.

Learn More

Use Cases