The ConcurrentStack and ConcurrentQueue classes, lock-free and thread-safe implementations of the Stack and Queue classes, build thread safe collections in .Net Thread safe collections were first introduced in .Net 4 with the introduction of the System.Collections.Concurrent namespace. The collection types in the System.Collections.Concurrent namespace contains a collection of thread safe collection classes. ConcurrentStack A stack is a data structure that works on LIFO (last in first out) basis. The ConcurrentStack class is a thread safe counterpart of the generic Stack class. The ConcurrentStack is a thread safe generic collection class that was first introduced as part of .Net Framework 4. Here’s the list of the important methods of this class that illustrate the possible operations. Push(T element) – this method is used to add data of type T. PushRange – this method can be used to add an array of items of type T. TryPop(out T) – this method is used to retrieve the first element from the stack. It returns true on success, false otherwise. TryPeek(out T) – this method is used to retrieve the next element from the stack but it doesn’t remove the element from the stack. Note that similar to the TryPop(out T) method, it returns true on success and false otherwise. TryPopRange – this method is overloaded and works similar to the TryPop but is used for retriving arrays from the stack Here’s how you can create an instance of the ConcurrentStack class and push data to it. ConcurrentStack<Int32> concurrentStack = new ConcurrentStack<Int32>(); for (Int32 index = 0; index < 10; index++) { concurrentStack.Push(index); } To retrieve the elements from out of a concurrent stack, you can leverage the TryPop(out T) method as shown below. Int32 data; bool success = concurrentStack.TryPop(out data); The following code listing illustrates how you can store and retrieve data to and from a concurrent stack. static void Main(string[] args) { ConcurrentStack<Int32> concurrentStack = new ConcurrentStack<Int32>(); for (Int32 index = 0; index < 100; index++) { concurrentStack.Push(index); } while (concurrentStack.Count > 0) { Int32 data; bool success = concurrentStack.TryPop(out data); if (success) { Console.WriteLine(data); } } Console.Read(); } When you execute the above code listing, the numbers 0 to 99 will be displayed in the reverse order at the console window. ConcurrentQueue A queue is a data structure that works on the basis of FIFO (first in first out). The ConcurrentQueue class in .Net acts as a thread safe FIFO based generic queue. The following is the list of the important methods in the ConcurrentQueue class. Enqueue(T element) – this method is used to add an item of type T to the queue TryPeek(out T) – this method is used to retrieve the next element from the queue but it doesn’t remove the element from the queue. This method returns true on success and false when it fails. TryDequeue(out T) – this method is used to retrieve the first element from the queue. Contrary to the TryPeek(out T) method, it removes the element from the queue. This method returns true on success and false otherwise. The following code snippet shows how you can create an instance of the ConcurrentQueue class to store integers. ConcurrentQueue<Int32> concurrentQueue = new ConcurrentQueue<Int32>(); To store elements to the concurrent queue instance you can take advantage of the Enqueue method as shown below. concurrentQueue.Enqueue(100); The following code listing illustrates how you can store and retrieve elements to and from a concurrent queue. ConcurrentQueue<Int32> concurrentQueue = new ConcurrentQueue<Int32>(); for (int index = 0; index < 100; index++) { concurrentQueue.Enqueue(index); } Int32 item; while (concurrentQueue.TryDequeue(out item)) { Console.WriteLine(item); } When you execute the above code listing, the numbers 0 to 99 will be displayed at the console window. Note that both ConcurrentStack and ConcurrentQueue classes are thread safe and they can manage locking and synchronization issues internally. You can also convert the concurrent queue instance to an array by making a call to the ToArray() method. The following code snippet illustrates how this can be achieved. ConcurrentQueue<Int32> concurrentQueue = new ConcurrentQueue<Int32>(); for (Int32 index = 0; index < 100; index++ ) concurrentQueue.Enqueue(index); Int32[] integerArray = concurrentQueue.ToArray(); foreach (int i in integerArray) { Console.WriteLine(i); } The IsEmpty property of the ConcurrentQueue class returns true is the collection is empty, false otherwise. The following code snippet shows how you can use this method. ConcurrentQueue<Int32> concurrentQueue = new ConcurrentQueue<Int32>(); for (Int32 index = 0; index < 100; index++ ) concurrentQueue.Enqueue(index); while(!concurrentQueue.IsEmpty) { Int32 result; concurrentQueue.TryDequeue(out result); Console.WriteLine(result); } Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos