Thread Safe List in C#

Muhammad Maisam Abbas Jun 03, 2021 Mar 29, 2021
  1. Thread Safe List With the ConcurrentBag Class in C#
  2. Thread Safe List With the ConcurrentQueue Class in C#
  3. Thread Safe List With the SynchronizedCollection Class in C#
Thread Safe List in C#

This tutorial will introduce the methods to create a thread-safe list in C#.

Thread Safe List With the ConcurrentBag Class in C#

The ConcurrentBag class is used to create a thread-safe, unordered collection of data in C#. The ConcurrentBag class is very similar to the List in C# and can be used as a thread-safe list in C#. To use the ConcurrentBag class, we have to import the System.Collections.Concurrent namespace in our project. Multiple threads can access a ConcurrentBag object simultaneously, but the contents inside the ConcurrentBag object can only be modified synchronously. It makes it both usable with multiple threads for concurrent operations and safe from accidental data loss. The following code example shows us how to create a thread-safe list with the ConcurrentBag class in C#.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace thread_safe_list
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcurrentBag<int> numbers = new ConcurrentBag<int>();
            numbers.Add(0);
            numbers.Add(1);
            numbers.Add(2);
            numbers.Add(3);
            numbers.Add(4);
            numbers.Add(5);
            Console.WriteLine("ConcurrentBag");
            foreach(var number in numbers)
            {
                Console.WriteLine(number);
            }
            List<int> nums = new List<int>();
            nums.Add(0);
            nums.Add(1);
            nums.Add(2);
            nums.Add(3);
            nums.Add(4);
            nums.Add(5);
            Console.WriteLine("List");
            foreach (var number in nums)
            {
                Console.WriteLine(number);
            }
        }
    }
}

Output:

ConcurrentBag
5
4
3
2
1
0
List
0
1
2
3
4
5

In the above code, we created a thread-safe list with the ConcurrentBag class in C#. The function to add new elements Add() is the same in both ConcurrentBag and List data structures. The only difference is that the List works on the principle of first in first out (FIFO), while the ConcurrentBag works on the principle of last in first out (LIFO). This problem is addressed in the code examples below.

Thread Safe List With the ConcurrentQueue Class in C#

The ConcurrentQueue class is used to create a thread-safe queue data structure in C#. The ConcurrentQueue works on the principle of first in, first out, just like the List in C#. The ConcurrentQueue object can be used instead of the List object to create a thread-safe data structure. See the following example code.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace thread_safe_list
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Concurrent Queue");
            ConcurrentQueue<int> numbers = new ConcurrentQueue<int>();
            numbers.Enqueue(0);
            numbers.Enqueue(1);
            numbers.Enqueue(2);
            numbers.Enqueue(3);
            numbers.Enqueue(4);
            numbers.Enqueue(5);
            foreach (var number in numbers)
            {
                Console.WriteLine(number);
            }
            Console.WriteLine("List");
            List<int> nums = new List<int>();
            nums.Add(0);
            nums.Add(1);
            nums.Add(2);
            nums.Add(3);
            nums.Add(4);
            nums.Add(5);
            foreach (var number in nums)
            {
                Console.WriteLine(number);
            }
        }
    }
}

Output:

Concurrent Queue
0
1
2
3
4
5
List
0
1
2
3
4
5

In the above code, we created a thread-safe List data structure with the ConcurrentQueue class in C#. There are quite a few differences between methods available in List and ConcurrentQueue data structures. For example, the method to add a new element in the List data structure is Add(). In contrast, the method to add a new element in the ConcurrentQueue data structure is Enqueue(), which is just like a conventional Queue data structure in C#. This drawback of the ConcurrentQueue data structure is addressed in the following example.

Thread Safe List With the SynchronizedCollection Class in C#

The SynchronizedCollection class is used to create a thread-safe collection of objects of some specified type in C#. The SynchronizedCollection data structure is very similar to the List data structure in C#. Both data structures work on the principle of first in, first out. The function to add a new element in both the SynchronizedCollection and the List data structures is Add(). See the following example.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace thread_safe_list
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Synchronized Collection");
            SynchronizedCollection<int> numbers = new SynchronizedCollection<int>();
            numbers.Add(0);
            numbers.Add(1);
            numbers.Add(2);
            numbers.Add(3);
            numbers.Add(4);
            numbers.Add(5);
            foreach (var number in numbers)
            {
                Console.WriteLine(number);
            }
            Console.WriteLine("List");
            List<int> nums = new List<int>();
            nums.Add(0);
            nums.Add(1);
            nums.Add(2);
            nums.Add(3);
            nums.Add(4);
            nums.Add(5);
            foreach (var number in nums)
            {
                Console.WriteLine(number);
            }
        }
    }
}

Output:

Synchronized Collection
0
1
2
3
4
5
List
0
1
2
3
4
5

In the above code, we created a thread-safe list with the SynchronizedCollection class in C#. So far, this approach is the best among the other two approaches for implementing the functionality of a list data structure in C# because it follows the same FIFO principle and has the same methods in it.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Csharp Thread

Related Article - Csharp List