Working with Dictionary in C#

The Diсtiоnаry<TKey, TVаlue> Сlаss in С# is а соlleсtiоn оf Keys аnd Vаlues. It is а generiс соlleсtiоn сlаss in the System.Соlleсtiоn.Generiсs nаmesрасe. The Diсtiоnаry <TKey, TVаlue> generiс сlаss рrоvides а mаррing frоm а set оf keys tо а set оf vаlues. Eасh аdditiоn tо the diсtiоnаry соnsists оf а vаlue аnd its аssосiаted key. Retrieving а vаlue by using its key is very fаst, сlоse tо О(1) beсаuse the Diсtiоnаry сlаss is imрlemented аs а hаsh tаble. Every key in а Diсtiоnаry <TKey, TVаlue> must be unique ассоrding tо the diсtiоnаry’s equаlity соmраrer.

Syntax:

Dictionary<string, string> dictionary = new Dictionary<string, string>();

Add element in the Dictionary

For adding elements we can use the method of Disctionary.Add();

Example:

Dictionary<string, string> dictionary = new Dictionary<string, string>();
// Add key value pair using Add()
dictionary.Add("1", "BMW");
dictionary.Add("2", "Audi");
dictionary.Add("3", "Toyota");
dictionary.Add("4", "Ford");

Access the value and Key of the dictionary

We can access the Key and value of Dictionary by any loop like for loop, for each loop.

Example:

Dictionary<string, string> dictionary = new Dictionary<string, string>();
// Add key/value pair using Add()
dictionary.Add("1", "BMW");
dictionary.Add("2", "Audi");
dictionary.Add("3", "Toyota");
dictionary.Add("4", "Ford");

// Print key/value pair
foreach (KeyValuePair<string, string> keyValuePair in dictionary)
{
	Console.WriteLine("Key = " + keyValuePair.Key + " Value = " +   keyValuePair.Value);
}

Output:

Key = 1 Value = BMW
Key = 2 Value = Audi
Key = 3 Value = Toyota
Key = 4 Value = Ford

Size/Count of Dictionary

To get the size of the Dictionary there's a property for the count that will count the total number of elements of the Dictionary.

Example:

Dictionary<string, string> dictionary = new Dictionary<string, string>();
// Add key value pair using Add()
dictionary.Add("1", "BMW");
dictionary.Add("2", "Audi");
dictionary.Add("3", "Toyota");
dictionary.Add("4", "Ford");

// Get count
Console.WriteLine(dictionary.Count);

Output:

4

Update Dictionary

Update the value of a key by entering a key in the indexer. It throws KeyNotFoundException, if a key is not found in the dictionary, use the ContainsKey () method before accessing unknown keys.

Dictionary<string, string> dictionary = new Dictionary<string, string>();
// Add key/value pair using Add()
dictionary.Add("1", "BMW");
dictionary.Add("2", "Audi");
dictionary.Add("3", "Toyota");
dictionary.Add("4", "Ford");

// Get count
dictionary["2"] = "KIA";

// Print key/value pair
foreach (KeyValuePair<string, string> keyValuePair in dictionary)
{
	Console.WriteLine("Key = " + keyValuePair.Key + " Value = " + keyValuePair.Value);
}

Output:

Key = 1 Value = BMW
Key = 2 Value = KIA
Key = 3 Value = Toyota
Key = 4 Value = Ford

Remove Dictionary Elements

The Remove() method removes an existing key/value pair from a dictionary. The Clear() method removes all entries from the dictionary.

Dictionary<string, string> dictionary = new Dictionary<string, string>();
// Add key/value pair using Add()
dictionary.Add("1", "BMW");
dictionary.Add("2", "Audi");
dictionary.Add("3", "Toyota");
dictionary.Add("4", "Ford");

// Check whether the key is present in the dictionary. If yes then remove it from the dictionarys
if (dictionary.ContainsKey("3"))
	dictionary.Remove("3");

// Print key/value pair
foreach (KeyValuePair<string, string> keyValuePair in dictionary)
{
	Console.WriteLine("Key = " + keyValuePair.Key + " Value = " + keyValuePair.Value);
}

Output:

Key = 1 Value = BMW
Key = 2 Value = KIA
Key = 4 Value = Ford