C# ConcurrentDictionary with Examples

C# ConcurrentDictionary with Examples

In the previous blog&nbsp;<a href="http://code-adda.com/2018/12/c-dictionary-with-examples/" target="_blank">C# Dictionary with Examples</a>, we have learned about C# Dictionary and how to use it for different purposes like Initialization, adding values, reading values, etc. In this article, we are going to learn about&nbsp;<strong>ConcurrentDictionary&nbsp;</strong>and how to use it for different purposes.

In the previous blog C# Dictionary with Examples, we have learned about C# Dictionary and how to use it for different purposes like Initialization, adding values, reading values, etc. In this article, we are going to learn about ConcurrentDictionary and how to use it for different purposes.

ConcurrentDictionary

Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads
concurrently.

The ConcurrentDictionary is a dictionary that allows you to add, fetch and remove items in a thread-safe way.

Note:

1. The ConcurrentDictionary type resides in System.Collections.Concurrent. It was introduced in .NET 4.0.

2. It makes adding, removing and updating values in a lookup table on multiple threads easier.

Before starting learning about ConcurrentDictionary, let us know why actually we need it and why we can not use C# Dictionary instead of C# ConcurrentDictionary. To make it clear please go through below code and try to write same in your VS and run it.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

class ConcurrentDict {
static void Main() { ConcurrentDictionary<int, string> con_dictionary = new ConcurrentDictionary<int, string>(); Dictionary<string, string> dictionary = new Dictionary<string, string>();

    Task t1 = Task.Factory.StartNew(() =&gt;
    {
        for (int i = 0; i &lt; 100; ++i)
        {
             con_dictionary.TryAdd(i, i.ToString());       

             // will generate error - System.ArgumentException: 'An item with the same key has already been added.'
            // dictionary.Add(i.ToString(), i.ToString()); 
        }
    });

    Task t2 = Task.Factory.StartNew(() =&gt;
    {
        for (int i = 0; i &lt; 100; ++i)
        {
             con_dictionary.TryAdd(i, i.ToString());                    
           // dictionary.Add(i.ToString(), i.ToString());
            Thread.Sleep(100);
        }
    });

    try
    {
        Task.WaitAll(t1,t2);
    }
    catch (AggregateException ex) // No exception
    {
        Console.WriteLine(ex.Flatten().Message);
    }
    Console.ReadLine();
}        

}

Just uncomment commented code – // dictionary.Add(i.ToString(), i.ToString()); and debug the code, There is error you’ll get stating “System.ArgumentException: ‘An item with the same key has already been added.'” which means in Dictionary once you allocate a key with the specific value you can never replace it. Fine! but what when you have to use Dictionary in the thread? In such a situation, you can use ConcurrentThread easily.

ConcurrentDictionary<TKey,TValue> provides some methods for performing different tasks like adding retrieving removing or updating key/value pairs in the dictionary

Add items in ConcurrentDictionary

TryAdd – We can use TryAdd method which basically attempts to add the specified key and value to the ConcurrentDictionary<TKey,TValue>

using System.Collections.Concurrent;

class ConcurrentDictionary { static void Main() { ConcurrentDictionary<int, string> dict = new ConcurrentDictionary<int, string>(); dict.TryAdd(1, "January"); //returns true dict.TryAdd(2, "February"); //returns true dict.TryAdd(3, "March"); //returns true

    dict.TryAdd(3, "April");    // returns false, means program will get execute but April will not replace March. 

    Console.ReadLine();
}        

}

Note:

TryAdd methods returns true if key/value pair is added, and

returns false if the key already exists in the dictionary

Updating Items in ConcurrentDictionary

TryUpdate – we can use TryUpdate method which basically checks whether the key has a specified value and if it does, updates the key with a new value. It is as same as  CompareExchange method, except that we use it for dictionary elements.


Note:

Compares two values for equality and, if they are equal, replaces the first value.

using System.Collections.Concurrent;

class ConcurrentDictionary { static void Main(string[] args) { ConcurrentDictionary<int, string> dict = new ConcurrentDictionary<int, string>(); dict.TryAdd(1, "January"); //returns true dict.TryAdd(2, "February"); //returns true dict.TryAdd(3, "March"); //returns true dict.TryAdd(4, "Apr"); //returns true

    string updatedValue;
    bool returnTrue = dict.TryUpdate(4, "April", "Apr"); //Returns true
    dict.TryGetValue(4, out updatedValue);
    Console.WriteLine(updatedValue); //Display "April"

    bool returnsFalse = dict.TryUpdate(4, "Apr", "Apr"); //Returns false
    dict.TryGetValue(4, out updatedValue);  //Returns "April" Old value
    Console.WriteLine(updatedValue);    //Returns "April"
}

}

Removing Item from ConcurrentDictionary

TryRemove – We can use TryRemove method which basically removes and return the value that has the specified key from them. It returns True if the object removed successfully and false if not.

static void Main(string[] args)
  {
      ConcurrentDictionary<int, string> dict = new ConcurrentDictionary<int, string>();
      dict.TryAdd(1, "January");   //returns true
      dict.TryAdd(2, "February");  //returns true
      dict.TryAdd(3, "March");     //returns true
      dict.TryAdd(4, "April");     //returns true

  string removedItem;
  bool result = dict.TryRemove(4, out removedItem); //Returns true
  Console.WriteLine(removedItem); //Returns "April"

}


by RAVI RANJAN KUMAR


c#

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look

C++ Tutorial for Beginners 19 - C++ Structures | Struct declaration | C++ Data Structures

Welcome to this course on C++ Tutorial for Beginners. In this video we will see How to use C++ Structures.

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

A Complete Guide to fread() in C/C++

In this article, we’ll take a look at using fread() in C/C++.