Creating Hashtables in C#

Creating a Hashtable Object with the Hashtable Class in C#

The hashtable class in C# is used to create a hash table. The syntax to create a new hash table is:

public Hashtable name_of_hashtable = new Hashtable ();

When you create a new hashtable, it is empty. Let’s create a hashtable called hashtableexample and place three integer keys inside it:

using System.Collections;
using System;
class Example
{
    static void Main()
    {
                Hashtable hashtableexample = new Hashtable();
                hashtableexample[1] = "One";
                hashtableexample[3] = "Three";
                hashtableexample[29] = "Twenty-Nine";
                foreach (DictionaryEntry entry in hashtableexample)
                {
                    Console.WriteLine("{0} : {1}", entry.Key, entry.Value);
                }
    }
}
Output:
29: Twenty-Nine
3: Three
1: One

In this program, we have created a class called Example to hold the hashtable named hashtableexample. We added three integer keys to that hashtable, with different entry key numbers and values. The integer keys we added belong to the DictionaryEntry type, which contains keys and their corresponding values. We added three keys with different key entries and values. In the final part of the program, we printed the keys with the corresponding values to the screen using the foreach loop.

It’ll be easier to learn the language as a whole instead of bits and pieces, so we recommend you check out this advanced course in C# to learn more about hashtables.

Adding Entries to a Hash Table

Now that we know how to add keys to a hash table, let’s see how to add entries to it. The syntax to add an item to a hashtable object is:

name_of_hashtable.Add(Parameter 1 (Data), Parameter 2 (Data))

You’d understand the syntax better with an example program. We’ll write a simple hashtable that contains the name of three authors and the number of books they’ve sold recently:

using System.Collections;
using System;
class Example
{
    static void main()
    {
                // Creating a simple hashtable called hashtableexample.
                Hashtable hashtableexample = new Hashtable();
                hashtableexample.Add("JKRownling", 5000);
                hashtableexample.Add("JArcher", 3000);
                hashtableexample.Add("AChristie", 1000);
    }
}

We created a simple hashtable with three entries inside it. As you can see, we used string values as well as integer values. Both your entries could have contained string or integer values. You can try writing the hashtable to the screen to see what it looks like (using the foreach loop, as we showed you earlier).

Hashtable Methods

There are several methods that you can use with the hashtable class. The Add method, for example, adds an entry to the hashtable class (like in the program above), the Clear method can be used to wipe the hashtable clean, the Clone method creates a copy (shallow) of your hashtable and the GetHash method returns the hashcode for a specific key. You will need to get familiar with most of the hashtable methods if you’re going to get a good grasp on hashtables and handle them properly. You can check out the different methods in the official documentation on the Microsoft Office website. Learn more about these methods in detail with this C# course.

We’ll write a simple program that demonstrates the use of the Contains method to help you understand the concept better. We’ll just extend the program we wrote earlier:

using System.Collections;
using System;
class Example
{
static Hashtable GetHashtable()
    {
                // Creating a simple hashtable called hashtableexample.
                Hashtable hashtableexample = new Hashtable();
                hashtableexample.Add("JKRowling", 5000);
                hashtableexample.Add("JArcher", 3000);
                hashtableexample.Add("AChristie", 1000);
                return hashtableexample;
   }
static void Main ()
{
Hashtable hashtableexample = GetHashtable ();
Console.WriteLine(hashtableexample.Contains (“JKRowling”));
}
Output:
True

The Contains method checks the hash table to see if a specific key is present. If it is, it will return a true value. If not, it returns a false value. We created a simple hash table called hashtableexample and then returned its value. We then checked to see if any of the keys in the hash table match “JKRowling” with the Contains method.

C# also has a newer, much more efficient class called the Dictionary collection which is quite similar in functionality. If you’re going to operate or maintain one of those programs for a company, you need to know about the hashtable class. The .NET framework makes C# sharp portable and platform independent, just like Java.  There is a growing demand for C# programmers in the industry and a shortage of good programmers. You may want to explore C# programming as a career option. This three part course gets into the details of C#(Part I, Part II, Part III) – from the basics to the advanced concepts – to help you along your journey.

Posted In CategoriesC#| Tagged Tags ,