Introduction

In this article you learn about the Dictionary collection, including how to use it and what the operations are that can be done on a Dictionary collection.

Dictionary

It is a collection of key and value pairs of data. It uses keys and values of a specified type including int and string. The key in the Dictionary should not be null but the value can be null. This class is defined in the following namespace that you can just import to use a Dictionary:

  1. using System.Collections.Generic;
Syntax

Dictionary<TKey, TValue>

Parameters

TKey: The type of key used in the Dictionary.
TValue: The type of value used in the Dictionary.

Adding Values

The Add method is used to add values to the Dictionary, it takes two parameters; one for the key and the other for the value.

Syntax
  1. var dic = new Dictionary<int, string>();
  2. dic.Add(1,"Krishna");
Here, in the above code dic is an object of Dictionary. The key type is int and the value type is string. It defines the krishna value added to the Dictionary.

Output

add record in dictionary


Removing Values

You can remove any key/value pairs from the Dictionary. Just pass the key value to the remove method.

Syntax
  1. var dic = new Dictionary<string, string>();
  2. dic.Add("K","Krishna");
  3. dic.Add("R", "Radha");
  4. dic.Add("J", "Jeetendra");
  5. dic.Add("S", "Sai");
  6. dic.Remove("S");
Here, we add 4 values to the Dictionary and finally remove the key value "S" from the Dictionary.

Output

remove record in dictionary


Count

This method is used to determine the total number of key/value pairs present in the Dictionary.

Syntax
  1. dic.Count();
Here, dic is the Dictionary parameter.

Output

record in dictionary


Clear

If you want to remove all the pairs from the Dictionary we used this method.

Syntax

  1. dic.Clear();
Retrive key/value pair from the Dictionary using a foreach loop
  1. foreach (KeyValuePair<string, string> item in dic)
  2. {
  3. Console.WriteLine("Key ->" + item.Key.ToString() + "Value ->" + item.Value.ToString());
  4. }
Here, KeyValuePair stores two values together, it is simple and available in the Dictionary. You can use var also replacing KeyValuePair.

Output

retrive values in dictionary


ContainsValue and ContainsKey

In the Dictionary class these two methods are used to search for a key or value in the Dictionary in a simple way.
  1. if (dic.ContainsKey("K"))
  2. {
  3. Console.WriteLine("Key is Present");
  4. }
  5. if (dic.ContainsValue("Jeetendra"))
  6. {
  7. Console.WriteLine("Value is Present");
  8. }
This is a simple example to search for a key or value (if present) in a Dictionary.

Output

key status

Demo Example

In this example you learn in detail about Dictionary.
  1. Program.cs
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Dictionary_Demo
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. //Creating Dictionary
  11. var dic = new Dictionary<string, string>();
  12. //Adding values to the Dictionary
  13. dic.Add("K","Krishna");
  14. dic.Add("R", "Radha");
  15. dic.Add("J", "Jeetendra");
  16. dic.Add("S", "Sai");
  17. Console.WriteLine("After Adding the record in Dictionary");
  18. //Retrive values using simple way
  19. foreach (var pair in dic)
  20. {
  21. Console.WriteLine("Key ->" + pair.Key);
  22. Console.WriteLine("Value ->" + pair.Value);
  23. }
  24. //Total number of pairs present in Dictionary
  25. var cnt = dic.Count;
  26. Console.WriteLine("Total Number of Pairs is " + cnt);
  27. //Removing values from Dictionary
  28. dic.Remove("S");
  29. Console.WriteLine("After Removing key in Dictionary");
  30. foreach (var pair in dic)
  31. {
  32. Console.WriteLine("Key ->" + pair.Key);
  33. Console.WriteLine("Value ->" + pair.Value);
  34. }
  35. Console.WriteLine("");
  36. //Retrive values using KeyValuePair
  37. Console.WriteLine("Retrive values using KeyValuePair");
  38. foreach (KeyValuePair<string, string> item in dic)
  39. {
  40. Console.WriteLine("Key ->" + item.Key.ToString() + "Value ->" + item.Value.ToString());
  41. }
  42. Console.WriteLine("");
  43. //Searching Key in Dictionary
  44. if (dic.ContainsKey("K"))
  45. {
  46. Console.WriteLine("Key is Present");
  47. }
  48. else
  49. {
  50. Console.WriteLine("Key is not Present");
  51. }
  52. Console.WriteLine("");
  53. //Searching Value in Dictionary
  54. if (dic.ContainsValue("Jeetendra"))
  55. {
  56. Console.WriteLine("Value is Present");
  57. }
  58. else
  59. {
  60. Console.WriteLine("Value is not Present");
  61. }
  62. //Remove all pairs from Dictionary
  63. dic.Clear();
  64. Console.WriteLine("Finish");
  65. Console.ReadKey();
  66. }
  67. }
  68. }
Note: For detailed code please download the Zip file attached above.

Summary

I hope you now understand the creation and use of a Dictionary. If you have any suggestion regarding this article then please contact me.