Dictionary in C# is a Generic collection class that holds key and value pair of data, unlike List class which stores strongly typed collection of objects.

The Dictionary<TKey, TValue> generally works as a look up table, which has TKey as unique key that allows to access TValue from it.

How to create a Dictionary in C#

You can create the Dictionary<TKey, TValue> object by defining the type of keys and values it can store. Following example shows how to create a dictionary.

In above example.

  • Dictionary<int, string> dictCity = new Dictionary<int, string>(); creates a dictionary that holds integer key and string values.
  • var dictState = new Dictionary<string, string>();  creates a dictionary that holds string key and string values.

Add item to Dictionary in C#

You can use the Add() method to add items to a dictionary, lets have a look at below example.

Add items to Dictionary using collection-initializer syntax

You can also use the collection-initializer syntax to create and add items to a Dictionary , lets have a look at below example.

In the above example, we have created dictionary of type Dictionary<int, string> and has int key and string values.

While using Dictionary, you should keep in mind that, dictionary only allows unique key, it doesn’t allow duplicate or null keys, however you can have value as null or duplicate.

Remove items from a Dictionary in C#

You can remove items from a dictionary using the Remove() method. This method takes the key as parameter and then removes the item from the Dictionary.

bool Remove(TKey key) :  Returns false if key is not found and true if the element is successfully found and removed.

You can also use Clear() method to remove all items present in a Dictionary.

Accessing items in a Dictionary in C#

You can use multiple ways to access items in dictionary.

Using Indexer : It can be accessed using indexer, specify a key to get the associated value. In below example we are accessing the value by using indexer syntax.

ElementAt(int index) : You can use this method to get a KeyValuePair<TKey, TValue>  from the specified index (index starts from 0) . In below example we are fetching elements present at 2nd and 3rd index.

ForEach Loop: You can use a for-each loop that enumerates elements of a dictionary and returns a variable of type KeyValuePair<TKey, TValue>. In below example we are using a for-each statement to loop through each item present in the dictionary.

TryGetValue(TKey key, out TValue value) : Using this method, you can fetch the Value of the specified key as out parameter. It returns true if key exists else returns false.

Accessing Dictionary items in C# example :

When you run above code then it produces below output.

Dictionary in C# OutPut 1.0

Update Dictionary item in C#

You can update the value of a key by specifying a key in the indexer. Lets have a look at below example, here we are updating key 4 & 5 values and then displaying the updated value.

If specified key is not present in the dictionary and we try to update/read the value from the dictionary, then it throws a KeyNotFoundException. In order to prevent this issue, in that case we can use the ContainsKey() method.

bool ContainsKey(TKey key): This method determines if a specified key present in the dictionary or not, it returns true if key is present or else false.

In below example, we are checking the existence of the key in the dictionary before updating its value.

Key Points of Dictionary in C#:

Some key points about dictionary.

  • It belongs to System.Collection.Generic namespace.
  • Dictionary only allows unique key, it doesn’t allow duplicate or null keys.
  • Dictionary allows null as value  or have duplicate values in it.
  • Attempting to read a value from a dictionary with a non existence key throws a KeyNotFoundException
  • When you enumerates dictionary elements then it exposes each element as a KeyValuePair<TKey, TValue> object.
  • The Dictionary<TKey, TValue> is implemented as a “Hash Table”, which provides a faster search access, if  searching is performed by using Key.

Thanks for your time, feel free to provide your feedback and do check my other blogs on Generics and List in C# – Collection Class.