
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Thread Safe Concurrent Collection in C#
The .NET Framework 4 brought the System.Collections.Concurrent namespace. This has several collection classes that are thread-safe and scalable. These collections are called concurrent collections because they can be accessed by multiple threads at a time.
The following are 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 |
Let us see how to work with ConcurrentStack<T> that is a thread-safe last in-first out (LIFO) collection.
Create a ConcurrentStack.
ConcurrentStack<int> s = new ConcurrentStack<int>();
Add elements
s.Push(1); s.Push(2); s.Push(3); s.Push(4); s.Push(5); s.Push(6);
Let us see an example
Example
using System; using System.Collections.Concurrent; class Demo{ static void Main (){ ConcurrentStack s = new ConcurrentStack(); s.Push(50); s.Push(100); s.Push(150); s.Push(200); s.Push(250); s.Push(300); if (s.IsEmpty){ Console.WriteLine("The stack is empty!"); } else { Console.WriteLine("The stack isn't empty"); } } }
- Related Articles
- Which collection classes are thread-safe in Java?
- How to make a collection thread safe in java?
- Thread-Safe collections in C#
- Make your collections thread-safe in C#
- Check if ArrayList is Synchronized (thread safe) in C#
- MongoDB concurrent update with sub collection?
- Is Swing thread-safe in Java?
- How to understand StringBuffer is thread-safe and StringBuilder is non-thread-safe in Java?\n
- Is Java matcher thread safe in Java?
- How to make a class thread-safe in Java?
- Do you think a Python dictionary is thread safe?
- Main thread vs child thread in C#
- Thread functions in C/C++
- Cracking the Safe in C++
- Thread Pools in C#

Advertisements