Concurrent dictionary is the advance version of dictionary that provides a big advantage over dictionary, that is Thread Safety. Concurrent dictionary allows multiple instances to access the object of concurrent dictionary. ConcurrentDictionary type resides in System.Collections.Concurrent. It was introduced in .NET 4.0.
In below example we shall add the search and then remove element from dictionary.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ConcurrentDictionary<int, string> empMap = new ConcurrentDictionary<int, string>();
empMap[1] = "Abhishek";//Add element to Dictionary
empMap[2] = "Amit";//Add element to Dictionary
string name = string.Empty;
if (empMap.ContainsKey(1)) //Find element in Dictionary using Key
empMap.TryRemove(1, out name);//Remove Element From Dictionary
if (!string.IsNullOrEmpty(name))
{
Console.Write(name);//Write the deleted Name
Console.Read();
}
}
}
}
0 Comment(s)