Skip to content

Hashtables

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!

Introduction

In this lesson, we will learn about Hashtables.

Which are one of the most useful collections in C#. We will see how to define them and how to use them and in which case scenarios we should take advantage of them.

To understand the concept of hashing and tables first think about a real dictionary

Assume we have a German/English Dictionary where every word in German corresponds to only one word in English. So when we try and translate the word ‘Auto’ in German, which translates to ‘Car’ in English, we used one word as a keyword to search, and we got the translation as a result.

This is exactly how hashtables and dictionaries in C# behave we use a key to get a value.

Another IT-related example is a student registration system where every student has an ID and many other data like name, age, grades, and picture. For such a system to work, we will need a special ID for every student. If we were to store this data using a hashtable or a dictionary, the student ID would be the Key, and the student data(Object containing all other student info).

would be the value

Section 1 Defining a Hashtable

While we are at it, let’s put our last example into action, and let’s implement a simple application that will create few objects of type ‘Student’ and store them into a hashtable using their IDs as keys and the object itself as a value.

First Let’s define our simple class with the following properties

  1. Id
  2. 2.Name
  3. 3.GPA

we could create the class in a separate file as Student.cs but for simplicity let’s define it outside our Program Class

let’s define our hashtable and initialize it as we do with any other object in the main method.

Section 2 Adding Entries to our Hashtable

Great Now we can define and initialize few objects of our Student class

After we initialize them we will add them to our Hashtable we will use our student Id property as a key and the object it self as the value like we discussed earlier

For that we will use the object method from the Hashtable called Add(),

Section 3 Fetching Entries from our Hashtable

Now let’s try fetching the data from our hashtable

We have a few options

The simplest one if we want to access an element that we already have its key is by accessing it using the hashtable object followed by [] just like an array

Since Hashtables are non-generic collections they will return an object which before we start working with we have to cast it back to type Student

Let’s assume we want to actually print all the Data inside of our hashtable and we don’t have a particular Key at our disposal, we can simply do that by getting all the keys from our hashtable

To solve this first we have to discuss a struct called DictionaryEntry, whenever we add a new entry to our Hashtable (Key-value pair) a new DictionaryEntry object will be created for us and it will get inserted into our hashtable which means that a hashtable is basically a collection of DictionaryEntrys so using a temporary object of DictionaryEntry we can go through our hashtable using a for each loop

Now as we discussed our values in a non-generic collection will always be stored as of type Object so we have to typecast it to student first, and to avoid multiple typecasting many times inside a loop

we will do that once at the start of each iteration.

Another less costly way would be to use a property of our hashtable called Values which will return a collection of values as it name implies and of course these values are of type object

Complete code.

Challange

Consider the following array of students with this class definition

Class Definition

Write a program that will iterate through each element of the student’s array and insert them into a hashtable

If a student with the same ID already exists in the hashtable skip it and display the following error:

“Sorry, A student with the same ID already Exists”

Hint: Use the method ContainsKey() to check whether a student with the same ID already exist

Solution:

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide