Dictionary in C# is a Generic collection class that holds key and value pair of data. A Dictionary<TKey, TValue> generally works as a look up table, that has TKey as unique key to access TValue from it.  If you haven’t gone through my blog on Generics in C# then please do it here.

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.

How to create a Dictionary in C#

You can create the Dictionary object by defining the type of key and value it can store. Following example shows how to create a dictionary.

In above example.

  • It creates a dictionary dictCity that holds integer key and string values.
  • dictState 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, as follows.

Add items to Dictionary using collection-initializer syntax :

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

In the above example, we have created Dictionary<int, string> that has int key and string value.

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.

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

Accessing items in a Dictionary in C#

There are multiple ways to access items in dictionary. Lets have a look at various options.

Using Indexer : We can use the Key to get the associated value. In below example we are accessing the value by using indexer syntax.

ElementAt(int index) : Using this method, we can 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: We 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, we 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.

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