
- 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
Make your collections thread-safe 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 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# −
Type | Description |
---|---|
BlockingCollection<T> | Bounding and blocking functionality for any type. |
ConcurrentDictionary<TKey,TValue> | Thread-safe implementation of a dictionary of key-value pairs. |
ConcurrentQueue<T> | Thread-safe implementation of a FIFO (first-in, first-out) queue. |
ConcurrentStack<T> | Thread-safe implementation of a LIFO (last-in, first-out) stack. |
ConcurrentBag<T> | Thread-safe implementation of an unordered collection of elements. |
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 −
ConcurrentStack<int> cs = new ConcurrentStack<int>(); cs.Push(95); cs.Push(120); cs.Push(130);
- Related Articles
- Thread-Safe collections in C#
- How to make a collection thread safe in java?
- How to make a class thread-safe in Java?
- Thread Safe Concurrent Collection in C#
- 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?
- Which collection classes are thread-safe in Java?
- Check if ArrayList is Synchronized (thread safe) in C#
- Do you think a Python dictionary is thread safe?
- How to Keep Your Home Wi-Fi Safe?
- 10 Ways to keep your Data safe in Cloud Environment
- Are Your NFTs Safe? The Basics of NFT Security
- How can you make your kid help in your household?
- Difference between Traditional Collections and Concurrent Collections in java

Advertisements