Hi Friends,

In this section I’m going to talk about Dictionary implementation. Now, Dictionary is the basic implementation of IDictionary. Before implementing Dictionary, let’s see the below code. This is very straight forward code having a collection printing month name and its short form.


however, in the above snippet i have used List. If i have to make use of Dictionary, then at that point i need to decide one item which can be treated as key. Below, i have made the same code as Dictionary implementation.

Now, when i see the output of this, it will print both key, value pairs in the [] brackets.


Now, if i hover on the var keyword in the foreach() loop, i will come to know that var is converted to name, value pair, hence it output the same.


however, i can get the same result with

but, this is fairly less readable, hence used var keyword, let compiler figure-out what type it is going to be. However, i can use any of the apis if i want to be specific like shown below.


and this will return me only values as shown below.


same thing i can do for Keys as well.

Now, when i have to look up any item in the dictionary then the code will be something like shown below.


However, this code holds true because i know the key because i have written that. How about the dictionary which some body gave you and you are not sure about the key like below, then it will throw the Key not found exception.


For this kind of scenario,we use below snippet. Here, TryGetValue() will look for the key and return the output in a month variable and based on that you can go ahead and print your value.


i can also modify the existing element based on the key as shown in the below snippet. This also proves the point for a given key, only one value can exist in the dictionary.


Now, lets consider a scenario, that you tried printing the value of b. However, in the Dictionary i have the same with B. In that case what will happen, it will return that Key not found. Now, to fix this issue, Dictionary also provides an option to compare the key before fetching. This option comes with interface IEqualityComparer. However, there is already built in string comparer class which compares the string. so, with the below signature it will fetch the value exactly same.


However, i can achieve the same by using interface as well. But, before that lets talk about hash code which is the underlying technology of Dictionary. Now, the way dictionary allocates the key inside in the memory is fairly complicated. Let’s say based on some algorithm they allocate a compartment of memory for a particular Key. Similarly, there can be different compartments for different Keys. However, same algorithm also repeats the compartment for a different key after reaching the threshold. So, a single compartment can have n no of keys. So, if you compare the performance of Dictionary with other collections like List, it is fairly efficient because based on the key it will first identify which compartment i have to look for to fetch the value.

Now, the importance of Hashcode is, dictionary uses hashcode to identify the compartment. Every object has a member function GetHashCode(). This function gets the hashcode of the object. hashcode here is 32bit integer. Now, let’s see whether below snippet works or not.


Now, this failed for a valid reason. so, “b”.GetHashCode()!= “B”.GetHashCode() because both are different strings. Since, b actually don’t exist, hence there is no question of getting same hash code for that. Now, the easiest fix around that is to redirect the hashCode to fetch the orignal one like this.


Now, lets consider a scenario where in want to sort the keys. For that i would use SortedList as shown below in the snippet.

and similarly, i can make the dictionary as readonlydictionary.

However, any attempt to modify the dictionary will give error here.


this was all about Dictionary. Thanks for joining me.

Happy Coding

This entry was posted in C# and tagged . Bookmark the permalink.

2 thoughts on “Dictionary

Comments are closed.