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


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

Variable Introduction in C#[Bangla]

LIKE | COMMENT | SHARE | SUBSCRIBE A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a speci...