C# | Dictionary Class

01 Sep, 2021
The Dictionary<TKey, TValue> Class in C# is a collection of Keys and Values. It is a generic collection class in the System.Collections.Generic namespace. The Dictionary <TKey, TValue> generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1) because the Dictionary class is implemented as a hash table. Every key in a Dictionary <TKey, TValue> must be unique according to the dictionary’s equality comparer.
Note: A key cannot be null, but a value can be if the value type TValue is a reference type.
Parameters:  
TKey : Denotes the type of the keys in the dictionary.
TValue : Denotes the type of the values in the dictionary.
Since the Dictionary<TKey, TValue> is a collection of keys and values, the element type is not the type of the key or the type of the value. Instead, the element type is a KeyValuePair <TKey, TValue> of the key type and the value type.  
Constructors
Example:
C#
// C# code to create a Dictionaryusing System;using System.Collections.Generic; class GFG {     // Driver code    public static void Main()    {         // Create a new dictionary of        // strings, with string keys.        Dictionary<string, string> myDict =          new Dictionary<string, string>();         // Adding key/value pairs in myDict        myDict.Add("1", "C");        myDict.Add("2", "C++");        myDict.Add("3", "Java");        myDict.Add("4", "Python");        myDict.Add("5", "C#");        myDict.Add("6", "HTML");         // To get count of key/value pairs in myDict        Console.WriteLine("Total key/value pairs"+              " in myDict are : " + myDict.Count);         // Displaying the key/value pairs in myDict        Console.WriteLine("\nThe key/value pairs"+                           " in myDict are : ");         foreach(KeyValuePair<string, string> kvp in myDict)        {            Console.WriteLine("Key = {0}, Value = {1}",                              kvp.Key, kvp.Value);        }    }}
Total key/value pairs in myDict are : 6

The key/value pairs in myDict are : 
Key = 1, Value = C
Key = 2, Value = C++
Key = 3, Value = Java
Key = 4, Value = Python
Key = 5, Value = C#
Key = 6, Value = HTML
 
Properties
Example 1:
C#
// C# code to get the keys// in the Dictionaryusing System;using System.Collections.Generic; class GFG {     // Driver code    public static void Main()    {         // Create a new dictionary of        // strings, with string keys.        Dictionary<string, string> myDict =          new Dictionary<string, string>();         // Adding key/value pairs in myDict        myDict.Add("1", "C");        myDict.Add("2", "C++");        myDict.Add("3", "Java");        myDict.Add("4", "Python");        myDict.Add("5", "C#");        myDict.Add("6", "HTML");         // To get count of key/value pairs in myDict        Console.WriteLine("Total key/value pairs"+              " in myDict are : " + myDict.Count);         // To get the keys alone, use the Keys property.        Dictionary<string, string>.KeyCollection keyColl =                                              myDict.Keys;         // The elements of the KeyCollection        // are strongly typed with the type        // that was specified for dictionary keys.        foreach(string s in keyColl)        {            Console.WriteLine("Key = {0}", s);        }    }}
Total key/value pairs in myDict are : 6
Key = 1
Key = 2
Key = 3
Key = 4
Key = 5
Key = 6
 
Example 2:
C#
// C# code to get the values// in the Dictionaryusing System;using System.Collections.Generic; class GFG {     // Driver code    public static void Main()    {         // Create a new dictionary of        // strings, with string keys.        Dictionary<string, string> myDict =           new Dictionary<string, string>();         // Adding key/value pairs in myDict        myDict.Add("1", "C");        myDict.Add("2", "C++");        myDict.Add("3", "Java");        myDict.Add("4", "Python");        myDict.Add("5", "C#");        myDict.Add("6", "HTML");         // To get count of key/value pairs in myDict        Console.WriteLine("Total key/value pairs"+              " in myDict are : " + myDict.Count);         // To get the values alone, use the Values property.        Dictionary<string, string>.ValueCollection valueColl =                                                myDict.Values;         // The elements of the ValueCollection        // are strongly typed with the type        // that was specified for dictionary values.        foreach(string s in valueColl)        {            Console.WriteLine("Value = {0}", s);        }    }}
Total key/value pairs in myDict are : 6
Value = C
Value = C++
Value = Java
Value = Python
Value = C#
Value = HTML
 
Methods
Example 1:
C#
// C# code to remove all entries//  from Dictionaryusing System;using System.Collections.Generic; class GFG {     // Driver code    public static void Main()    {         // Create a new dictionary of        // strings, with string keys.        Dictionary<string, string> myDict =          new Dictionary<string, string>();         // Adding key/value pairs in myDict        myDict.Add("1", "C");        myDict.Add("2", "C++");        myDict.Add("3", "Java");        myDict.Add("4", "Python");        myDict.Add("5", "C#");        myDict.Add("6", "HTML");         // To get count of key/value pairs in myDict        Console.WriteLine("Total key/value pairs "+                "in myDict are : " + myDict.Count);         myDict.Clear();         // To get count of key/value pairs in myDict        Console.WriteLine("Total key/value pairs in "+             "myDict after Clear() operation are : " +                                        myDict.Count);    }}
Total key/value pairs in myDict are : 6
Total key/value pairs in myDict after Clear() operation are : 0
 
Example 2:
C#
// C# code to remove the entry with// the specified key from the Dictionaryusing System;using System.Collections.Generic; class GFG {     // Driver code    public static void Main()    {         // Create a new dictionary of        // strings, with string keys.        Dictionary<string, string> myDict =          new Dictionary<string, string>();        // Adding key/value pairs in myDict        myDict.Add("1", "C");        myDict.Add("2", "C++");        myDict.Add("3", "Java");        myDict.Add("4", "Python");        myDict.Add("5", "C#");        myDict.Add("6", "HTML");         // To get count of key/value pairs in myDict        Console.WriteLine("Total key/value pairs "+                "in myDict are : " + myDict.Count);         // Remove the entry with the        // specified key from the Dictionary        myDict.Remove("Russia");         // To get count of key/value pairs in myDict        Console.WriteLine("Total key/value pairs in"+          " myDict after Remove() operation are : " +                                       myDict.Count);    }}
Total key/value pairs in myDict are : 6
Total key/value pairs in myDict after Remove() operation are : 6
 
Reference: 
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.7.2
 
mosheborisute
CSharp Dictionary Class
CSharp-Generic-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.