top of page

C# Programming Language: Hashtables



Hashtables can be used to store a collection of data, similar to that of a normal list. The main difference is that a Hashtable can store elements as key/value pairs as an alternative to using index numbers. The keys must be unique and cannot be null. The values can be duplicates or be null. With this introduction out of the way, let’s get into it.


Creating Hashtables

Let’s start by creating a Hashtable. Before we can create a Hashtable, we need to make sure that we have our Hashtable import, which is included within the System.Collections package:

using System.Collections;

Now, we can successfully create our Hashtable:

Hashtable myHT = new Hashtable();

We have created our Hashtable and named it ‘myHT’.


Adding Elements Into The HashTable

Now, let’s see how we can add elements into the Hashtable. We can do this by using the built in Add() function that is provided. Let’s see an example:

Hashtable myHT = new Hashtable();
myHT.Add(1, "One");
myHT.Add(4, "Four");
myHT.Add(17, "Seventeen");
foreach(var key in myHT.Keys)
{
    Console.WriteLine(key + ": " + myHT[key]);
}

Output:
4: Four
17: Seventeen
1: One

In the example above, we can see that we added a few elements into our Hashtable by using the Add() function. We can verify that they were successfully added by printing our Hashtable and checking the output.


As we can also see in the example above, we are also able to successfully retrieve the keys and values of all the elements as well.


Checking The Size of The Hashtable

Now, let’s see how we can check the size of the Hashtable, or how many elements are in the Hashtable. We can do this by using the built in ‘Count’ property that is provided. Let’s see an example:

Hashtable myHT = new Hashtable();
myHT.Add(1, "One");
myHT.Add(4, "Four");
myHT.Add(17, "Seventeen");
Console.WriteLine(myHT.Count);

Output:
3

In the example above, we can see that we added a few elements into our Hashtable. We then printed out the result of the count property on our Hashtable and we can check the output to see how many elements we have.


Checking if The Hashtable Contains a Specific Key

Now, let’s try to see if our Hashtable contains a specific key. We can do this by using the built in ContainsKey() function that is provided. Let’s see an example:

Hashtable myHT = new Hashtable();
myHT.Add(1, "One");
myHT.Add(4, "Four");
myHT.Add(17, "Seventeen");
Console.WriteLine(myHT.ContainsKey(1));
Console.WriteLine(myHT.ContainsKey(2));

Output:
True
False


In the example above, we can see that we added a few elements into our Hashtable. Then we printed the results of our ContainsKey() functions. We checked to see if our Hashtable contains the keys of ‘1’ and ‘2.’ As we can see in the output, our Hashtable contains a key of ‘1,’ but not ‘2.’


Checking if The Hashtable Contains a Specific Value

Now, let’s try to see if our Hashtable contains a specific value. We can do this by using the built in ContainsValue() function that is provided. Let’s see an example:

Hashtable myHT = new Hashtable();
myHT.Add(1, "One");
myHT.Add(4, "Four");
myHT.Add(17, "Seventeen");
Console.WriteLine(myHT.ContainsValue("One"));
Console.WriteLine(myHT.ContainsValue("Two"));

Output:
True
False

In the example above, we can see that we added a few elements into our Hashtable. Then we printed the results of our ContainsValue() functions. We checked to see if our Hashtable contains the values of ‘One’ and ‘Two.’ As we can see in the output, our Hashtable contains a value of ‘One,’ but not ‘Two.’


Removing an Element From a Hashtable

Now, let’s see how we can remove an element from a Hashtable. We can do this by using the built in Remove() function that is provided. Let’s see an example:

Hashtable myHT = new Hashtable();
myHT.Add(1, "One");
myHT.Add(4, "Four");
myHT.Add(17, "Seventeen");
myHT.Remove(1);
foreach(var key in myHT.Keys)
{
    Console.WriteLine(key + ": " + myHT[key]);
}

Output:
4: Four
17: Seventeen

In the example above, we can see that we added a few elements into our Hashtable. Then we used the Remove() function to remove the element with the key of ‘1’ from our Hashtable. We can verify that we successfully removed the corressponding element by printing our Hashtable and checking the output.


Clearing out a Hashtable

Now, let’s see how we can completely clear out a Hashtable. We can do this by using the built in Hashtable() function that is provided. Let’s see an example:

Hashtable myHT = new Hashtable();
myHT.Add(1, "One");
myHT.Add(4, "Four");
myHT.Add(17, "Seventeen");
myHT.Clear();
foreach(var key in myHT.Keys)
{
    Console.WriteLine(key + ": " + myHT[key]);
}

In the example above, we have added a few elements into our Hashtable. Then we used the Clear() function on our Hashtable. We can verify that all elements were removed by printing our Hashtable and checking the output. Our output should be empty because we removed everything.




Resource: Medium - Jesse L


The Tech Platform

0 comments

Comments


bottom of page