Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Add, Search and Remove Items from Concurrent Dictionary

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 988
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: