Hashtable
Share:
In C#, Hashtable is a non-generic collection class that stores (Key, Value) pairs. It is somewhat similar to a generic dictionary, but whereas dictionaries ensure that each key in the collection is unique, hashtables do not necessitate unique keys. Therefore, Hashtable allows you to hold objects in such a way that you can access their values by using their keys.
In this chapter, we will walk you through several key aspects of Hashtables, including creating a Hashtable, adding items to it (with non-unique keys), accessing values, modifying values, removing items, and looping through a Hashtable.
Creating a Hashtable
First off, let's start with how to create and initialize a Hashtable. Instantiate a new Hashtable like any other object in C#:
Hashtable movieCharacters = new Hashtable();
Here we've created a Hashtable named movieCharacters.
It’s currently empty as we've just initialized it without any elements.
Adding Items to Hashtable
To add items into the Hashtable, we use the Add()
method to which we need to supply a unique key and value:
movieCharacters.Add("TH001", "Thanos");
movieCharacters.Add("IM001", "Iron Man");
movieCharacters.Add("CAP001", "Captain America");
In this case, elements are the paired combinations of key and value. The keys ("TH001", "IM001", "CAP001") represent the codenames of movie characters (Thanos, Iron Man, and Captain America). These are merely placeholders; you can use whatever keys or values you like.
Accessing Values from Hashtable
The value of an entry in a Hashtable can be obtained by mentioning its key inside the square brackets []:
string characterName = (string) movieCharacters["TH001"];
Console.WriteLine(characterName); //Outputs: Thanos
The above snippet will retrieve the value corresponding to the key "TH001" which is "Thanos".
Modifying Values in Hashtable
To change or modify a value in the Hashtable, use the assignment operator (=). The new value will replace the old value:
movieCharacters["IM001"] = "Robert Downey Jr.";
After this line of code is executed, the value corresponding to the key "IM001" will no longer be "Iron Man". It has now been replaced with "Robert Downey Jr."
Removing Items from Hashtable
To remove a certain item from Hashtable, use the Remove()
method:
movieCharacters.Remove("CAP001");
This line of code will remove the "CAP001" and associated value "Captain America" from the Hashtable.
Traversing through a Hashtable
There are several ways of traversing through or printing all the keys and values of a Hashtable.
1. Using a foreach loop:
foreach (DictionaryEntry character in movieCharacters)
{
Console.WriteLine("Code: {0}, Character: {1}", character.Key, character.Value);
}
The foreach
statement is used to iterate through the Hashtable where each KeyValuePair in the Hashtable is represented by the DictionaryEntry object.
2. Using an enumerator:
IDictionaryEnumerator enumerator = movieCharacters.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine("Code: {0}, Character: {1}", enumerator.Key, enumerator.Value);
}
The GetEnumerator
retrieves an IDictionaryEnumerator that you can use to iterate through the Hashtable.
Conclusion
The Hashtable, an often-underused but powerfully flexible collection class in C#, enables developers to handle and manipulate data in unique and efficient ways, provided it’s used correctly. Understanding how to effectively implement Hashtables in your code will enhance your ability to manage data and provide a solid foundation for more complex tasks and operations in your applications.
This tutorial covered the basics of Hashtables, including creating and initializing a Hashtable, adding items to it, accessing values, modifying values, removing items, and looping through a Hashtable. Remember to practice and experiment with Hashtables on your own to get a thorough understanding of them, they will prove to be a useful tool in your C# programming journey!
0 Comment
Sign up or Log in to leave a comment