Dictionary

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.

34th

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.

35th

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.

36th

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.

36th

and this will return me only values as shown below.

34th

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.

38th

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.

39th

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.

40th

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.

41th

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.

42nd

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.

43rd

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.

42nd

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.

44th

this was all about Dictionary. Thanks for joining me.

Thanks,
Rahul
Happy Coding

Stack and Queue

Hi Friends,

Now in this section, I am going to talk about Stack. Basically, stack works on the principle of LIFO(Last in First out) means the element which gets inserted in the stack Last will come out first. Here, there are two fundamental principles involved 1)Push and 2)Pop. Push is to insert the element on the stack and Pop to remove the same.

Below, in the snippet i have explained all the basic operations involved with Stack.

33rd

Now, lets cover Queue. Queues are opposite of Stack. Queues work on the principle of FIFO(First in First out). Here, also two basic operations are involved to insert(known as Enqueue) and remove(known as Dequeue). Lets see the same in action.

33rd

So, this was the stacks and queue. In the next section, will cover some other collection. Till then stay tuned and Happy Coding.

Thanks,
Rahul Sahay
Happy coding

Posted in C# | Tagged

LinkedList

Hi Friends,

Today in this section, i am going to talk about Linked Lists. But lets 1st understand Linked Lists here. The purpose of a linked list is to provide a collection where in you can add/remove items very easily. Linked list are having one address section apart from data section as well where in it stores the reference of next node and so on. In case of doubly linked list. on both the ends there are previous and next references of data element, so it can move in the list to and fro.
There are certain features of linked list:-

    .

  • Adding/Removing element is fast.
  • Good at enumeration
  • No index based access to elements.
  • Linked Lists are not very good when it comes for memory consumption.

Now, in the below example I have a linked List code. But, one thing to remember here that with Linked List you can’t use collection initializer to set up values. Below in the screen shot i have highlighted the APIs which Linked Lists offer out of the box.

31st

so, if you see the code then i have missed the month march in the collection. Now, lets suppose if i have to add the same in the collection, then how do we do that. So, for doing the same, what we need to do is go ahead and use the API AddAfter on element as shown below in the snippet. Now, one more point to note here that LinkedList is a collection of LinkedListNode which means i need to find the reference item 1st and then pass the same to the constructor.

then, this will produce the desired output as shown below.

32nd

However, one point to note here this is not the efficient way of using LinkedList although AddAfter() is very efficient because in this case we have used Find() method which will scan the entire list until it finds the desired element. But, in this case i had no choice as i didn’t keep the reference stored upfront.Also, one more important point to note here that if Linked List contains the same value more than once in the list, then Find() will only find the 1st one.

Now, for removing the node you can use Remove(“”), but again this is not very efficient as this is going to scan the List. However, you can also directly use RemoveFirst() or RemoveLast() if your intention to remove the item that one or you can pass the reference of that node likewise. So, this was the Linked List. In the next section, we’ll cover some other collection.

Thanks,
Rahul Sahay
Happy Coding

Posted in C# | Tagged