ASP.NET HashTable

A Hashtable is a data structure that embodies a sophisticated collection of key/value pairs, skillfully associating keys with their corresponding values. It serves as a mechanism for mapping unique keys to specific values. In this structure, keys hold major importance and can be assigned any non-null object, providing utmost flexibility. Conversely, values within the Hashtable must possess the ability to store any object, ensuring versatility in accommodating various data types.

To access the stored information, one can elegantly retrieve items from the Hashtable by referencing their respective keys. This retrieval process ensures a seamless and efficient means of accessing the desired values within the collection. It is worth noting that both the keys and the values themselves are treated as Objects, allowing for a wide range of data to be employed and manipulated within this framework.

Add a pair of values in a HashTable

Dim ht As New Hashtable ht.Add("1", "Sunday") ht.Add("2", "Monday")
Hashtable ht = new Hashtable(); ht.Add("1", "Sunday"); ht.Add("2", "Monday");

Check if a specified key exist or not


The above code return true if item exist else false.

Remove the specified Key and corresponding Value

HashTable.Remove(Key) VB.Net

Display all key value pairs

For Each item In ht Label1.Text = item.Key & " -- " & item.Value Next
foreach (DictionaryEntry item in ht) { Label1.Text = item.Key + " - " + item.Value; }

The following ASP.NET program add seven days in a week to a HashTable and bind it to a ListBox control.


<html xmlns=""> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <br /> <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox><br /> </div> </form> </body> </html>
Full Source | C#
using System; using System.Collections; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { Hashtable ht = new Hashtable(); ht.Add("1", "Sunday"); ht.Add("2", "Monday"); ht.Add("3", "Tuesday"); ht.Add("4", "Wednesday"); ht.Add("5", "Thursday"); ht.Add("6", "Friday"); ht.Add("7", "Saturday"); ListBox1.DataSource = ht; ListBox1.DataValueField = "Key"; ListBox1.DataTextField = "Value"; ListBox1.DataBind(); } }
Full Source | VB.NET
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ht As New Hashtable ht.Add("1", "Sunday") ht.Add("2", "Monday") ht.Add("3", "Tuesday") ht.Add("4", "Wednesday") ht.Add("5", "Thursday") ht.Add("6", "Friday") ht.Add("7", "Saturday") ListBox1.DataSource = ht ListBox1.DataValueField = "Key" ListBox1.DataTextField = "Value" ListBox1.DataBind() End Sub End Class


A Hashtable is a remarkable tool that facilitates the organization and management of key/value pairs, enabling the retrieval of stored items through key-based referencing. It empowers developers to handle complex data structures with ease, as it accommodates diverse objects as keys and values, thereby enhancing its overall flexibility and applicability.