.NET Collections - Generic Collection - Dictionary<Tkey, TValue>

In this blog, you will learn how to create a Dictionary and perform Add, Remove, Sort, Search, and other operations on a List.

Dictionary is a generic collection used to store key/value pairs. Dictionary's work is very similar to the non-generic hashtable. Dictionary is defined under System.Collection.Generic namespace. The dictionary's size increases as needed. The Dictionary class uses interface, IReadOnlyCollection < KeyValuePair < TKey, TValue > Interface, IReadOnlyDictionary < TKey, TValue > Interface, and IDictionary Interface.
 

Key Points about Dictionary

  1. Key in a dictionary cannot be null; however, a value can be.
  2. Key in the dictionary must be unique.
  3. Dictionary is strongly typed, this means the dictionary accepts key/value of only the same types are defined.
Please refer to the below code which shows all the operations we can perform on Dictionary<TKey,TValue>.
  1. // Creating a dictionary       
  2. // using Dictionary<TKey,TValue> class       
  3. Dictionary<intstring> empList =    
  4.                new Dictionary<intstring>();    
  5.   
  6. // Add() method - For Adding key/value pairs in the Dictionary       
  7. // Employee Code and Name    
  8. empList.Add(1, "X");    
  9. empList.Add(2, "Y");    
  10. empList.Add(3, "Z");    
  11.   
  12. foreach (KeyValuePair<intstring> emp in empList)    
  13. {    
  14.     Console.WriteLine($"Employee Code - {emp.Key}, Employee Name - {emp.Value}");    
  15. }    
  16.   
  17. // Creating another dictionary     
  18. // using Dictionary<TKey,TValue> class     
  19. // adding key/value pairs without     
  20. // using Add method  - Department/EmpName    
  21. Dictionary<stringstring> empList2 =    
  22.         new Dictionary<stringstring>(){    
  23.                       {"IT""Rahul"},    
  24.                       {"Dev""Raj"},    
  25.                     {"HR""Ayesha"} };    
  26.   
  27. foreach (KeyValuePair<stringstring> emp in empList2)    
  28. {    
  29.     Console.WriteLine($" Department - {emp.Key}, Employee Name - {emp.Value}");    
  30. }    
  31.  
  32. #region Check Element/Value is Present    
  33.   
  34. // Using ContainsKey() method to check     
  35. // the specified key is present or not     
  36. if (empList.ContainsKey(3) == true)    
  37. {    
  38.     Console.WriteLine("Key is found...!!");    
  39. }    
  40.   
  41. else    
  42. {    
  43.     Console.WriteLine("Key is not found...!!");    
  44. }    
  45.   
  46. // Using ContainsValue() method to check     
  47. // the specified value is present or not     
  48. if (empList.ContainsValue("Z") == true)    
  49. {    
  50.     Console.WriteLine("Value is found...!!");    
  51. }    
  52.   
  53. else    
  54. {    
  55.     Console.WriteLine("Value is not found...!!");    
  56. }    
  57.  
  58. #endregion    
  59.  
  60. #region Remove Elements    
  61.   
  62. // Using Remove() method      
  63. empList.Remove(2);    
  64.   
  65. // After Remove() method     
  66. foreach (KeyValuePair<intstring> emp in empList)    
  67. {    
  68.     Console.WriteLine($"Employee Code - {emp.Key}, Employee Name - {emp.Value}");    
  69. }    
  70. Console.WriteLine();    
  71.   
  72.   
  73. // Using Clear() method     
  74. empList.Clear();    
  75.   
  76. Console.WriteLine($"Total key/value pairs present in empList:{empList.Count}");    
  77.  
  78. #endregion