Hashtable in C# is a collection of data in a form of key-value pairs, which are based on the hash code of the key and the key is used in order to access the elements or the data within the collection. It is inherited from the Objects Class to Hashtable. So, basically, hashtable in C# or any programming language is a simple representation of pairs of keys and values, which are properly organized in a hash code format.
Syntax:
Now that we know what a C# hashtable is, let’s move on to understanding the standard syntax for proper implementation of the hashtable. Below are the standard syntax and required system files for using hashtable in a program.
using System.Collections; Hashtableht = new Hashtable();
The system file, that contains the collections is responsible for importing required functions and methods that the hashtable employs. Then the hashtable is the main keyword here, we created an instance as ht, and our operations will be performed over the newly created ht. Now that we know the proper syntax for the implementation of a hashtable, let us understand how it works.
As explained earlier, we know that a hashtable is a collection of data or information in a form of key-value pairs. A simple example for a key value pair is “Name: Sulaksh”, here the key is Name and the value is Sulaksh, the key stays the same while the values can be different. Hashtable consists of key and value, which are represented with curly brackets and use a hash function to compute.
Let us now move towards proper implementation of the hashtable and understand the working into examples.
Our first example is a simple implementation of a hashtable, where we have a simple hashtable, with a number keys and values and we will print the total number of elements we have in the hashtable, the code is as follows:
Code:
using System; using System.Collections; class sampleht { static public void Main() { Hashtableexampleht = new Hashtable(); exampleht.Add(1, " Element 1"); exampleht.Add(2, " Element 2"); exampleht.Add(3, " Element 3"); Console.WriteLine("\n The Total No. of elements: {0}", exampleht.Count); } }
Code Explanation: Started with system files, here the collections are most important, then we have our class, within which is our main method. Within the main method, is our declaration of a hashtable, followed by three key-value pairs. We have implemented the add function to insert the elements. So our hashtable consists of three key-value pairs and finally, we have a print statement, which will print the total number of elements we have within our hashtable, which is three. We are using a simple count function here, refer the below-attached screenshot for output:
As expected the output tells us that we have four elements into our hashtable, now moving on to our next example, we will attempt to show the key and values of the hashtable.
Code:
using System; using System.Collections; class exampleHT { static publicvoid Main() { HashtablesampleHT = new Hashtable(); sampleHT.Add(1, " One"); sampleHT.Add(2, " Two"); sampleHT.Add(3, " Three"); Console.WriteLine("\n Below is the content of Hashtable: \n"); foreach (DictionaryEntry entry in sampleHT) { Console.WriteLine(" {0}, {1}", entry.Key, entry.Value); } } }
Code Explanation: Similar to the earlier example, we have system files and class with the main method within. Then we have our hashtable, followed by the key value pairs and then a print statement. Then we have our foreach statement, which will select one element at a time, and in the next line will print it as output. Our output is expected to be a list of elements in the form of key and values, refer to the below screenshot.
As expected, the output is printing the elements of the hashtable and now for our next example we will implement the clear function with outhashtable and the code is as follows.
Code:
using System; using System.Collections; class sampleht { static public void Main() { Hashtableexampleht = new Hashtable(); exampleht.Add(1, " Element 1"); exampleht.Add(2, " Element 2"); exampleht.Add(3, " Element 3"); Console.WriteLine("\n Elements of the Hashtable :"); foreach(DictionaryEntry ele1 in exampleht) { Console.WriteLine(ele1.Value); } Console.WriteLine(" No. of elements before clearing: {0} ", exampleht.Count); exampleht.Clear(); Console.WriteLine(" Total No. of elements now: {0} ", exampleht.Count); } }
Code Explanation: With our required system files and methods, we have our hashtable and total of three key value pairs defined within. We have implemented the add function to add the elements to the hashtable Then we our print statement, which will simply print all the elements within the hashtable, which is three. Before clearing the hashtable, we have printed the total number of elements present in the list and then we have our clear function, which will clear the whole hashtable, meaning every element will be cleared off the list and the final print statement will print the number of elements present now, which will be zero.
As explained, the clear function does it’s work, and the list is cleared and now, moving on, for our next example, we will implement the remove function.
Code:
using System; using System.Collections; class sampleht { static public void Main() { Hashtableexampleht = new Hashtable(); exampleht.Add(1, " Element 1"); exampleht.Add(2, " Element 2"); exampleht.Add(3, " Element 3"); Console.WriteLine("\n List of Original Elements:"); foreach (var key in exampleht.Keys ) Console.WriteLine(" {0}, {1}",key , exampleht[key]); exampleht.Remove(3); exampleht.Remove(1); Console.WriteLine("\n Elements after removal:"); foreach (var key in exampleht.Keys ) Console.WriteLine(" {0}, {1}",key , exampleht[key]); } }
Code Explanation: Just as our earlier examples, we have our system files, class, and the main method. Then we have our hashtable with a total of three key values. Then we have our print statement, which will print out the original list along with key and values, using foreach. We then have the remove function for two keys, which will remove the keys that we pass, then we have our print statement, which will use foreach and print every single element present in the hashtable after removing.
Advantages of any function or methodology is important to understand its real time applications and hashtable’s most widely known advantage is how it allows the developer the ability for synchronization. Hashtable has noted advantages over the search tree and this makes them practical to use in real time computer applications and for the purpose of database indexing. Speed, compared to other table type data structures is best delivered by hashtables. Having key value pairs makes it easy to anticipate the format regarding data and restructuring is done easily.
Hashtable in C# is a collection of elements, represented in a key value pair format. The key can be the same, while values differ and the key cannot be null while a value can be. We implemented an example of functions like remove, clear, and print. Hashtable has a speed advantage over other data structures.
The above is the detailed content of C# Hashtable. For more information, please follow other related articles on the PHP Chinese website!