The things I learnt while I migrated from classic asp to is given here for my reference as well as for new beginners.


What is Hash Table in ASP.NET?

What is the Equivalent of Scripting.Dictionary of Classic ASP?



The Hash Table will look like this inside the memory:


2345 raja
8436 roja
9867 pooja

Important points about HashTable

Once you are able to create such a hash table, then you can use it as a datasource.

The KEY field should NOT be NULL. But the value field can be NULL.

System.Collection should be imported. Because it cotains the class "HashTable".

Where exactly I can use HashTable? When you create a DataTable using an SQL statement, then the DataTable can be directly bind to a Datagrid. But in certain cases, after fetching the recordset from the Database, you have to do some arithmetic operation,etc and then only you have to create a datasource. Under such conditions, HashTable will be useful.

Sample C#code which uses HashTable

<%@ Page Language="C#"%>
using System.Collections;

<script Language="C#" runat="server">
void Page_Load() {
if (!Page.IsPostBack){
empList=new HashTable;
radioOutput(s as Object,e As EventArgs){
Lable1.text="Selected Employee is : " & RadioButton1.SelectedItem.Text

<form runat="server">
<asp:RadioButtonList id="RadioButton1" runat="server"
AutoPostBack="True" onSelectedIndexChanged="adioOutput" />
<br />
<asp:Label id="Label1" runat="server" />

Some useful commands:

int cnt =HashTable1.Count;


HashTable1[ 2222" ] = "ravi";

If 2222 is not there, then a new record will be created.



if (HashTable1.ContainsKey("Key1"))


if (HashTable1.ContainsValue("Value1"))


IDictonaryEnumerator is an interface for Dictonary objects.

IDictionaryEnumerator List1= HashTable1.GetEnumerator();

while (List1.MoveNext())
response.write(List1.Key + "..." + List1.Value + "<br />";

The above while loop will NOT display the items in the order YOU have entered. Because, the items are stored based on the hash value of the KEY. So HashTable is not good for just displaying all the records. but it works very fast in situations where you will supply the KEY and It will give the VALUE.


What is ListDictionary and HybridDictionary?

HashTable class may be used when the collection is large
for performance reasons similarly for small collections
ListDictionary class is used.HybridDictionary Class is a mix
of both the worlds.HybridDictionary Class implements
IDictionary by using a ListDictionary while the collection
is small, and then switching to a Hashtable when the
collection gets large.

Advanced Information on How HashTable works?

When an element is added to the Hashtable, the element is placed into a bucket based on the hash code of the key. Subsequent lookups of the key use the hash code of the key to search in only one particular bucket, thus substantially reducing the number of key comparisons required to find an element. The load factor of a Hashtable determines the maximum ratio of elements to buckets. Smaller load factors cause faster average lookup times at the cost of increased memory consumption. The default load factor of 1.0 generally provides the best balance between speed and size.

A different load factor can also be specified when the Hashtable is created. As elements are added to a Hashtable, the actual load factor of the Hashtable increases. When the actual load factor reaches the specified load factor, the number of buckets in the Hashtable is automatically increased to the smallest prime number that is larger than twice the current number of Hashtable buckets.

Each key object in the Hashtable must provide its own hash function, which can be accessed by calling GetHash. However, any object implementing IHashCodeProvider can be passed to a Hashtable constructor, and that hash function is used for all objects in the table. The capacity of a Hashtable is the number of elements the Hashtable can hold. As elements are added to a Hashtable, the capacity is automatically increased as required through reallocation. The foreach statement of the C# language requires the type of each element in the collection.

Since each element of the Hashtable is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is DictionaryEntry. The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection. Because serializing and deserializing an enumerator for a Hashtable can cause the elements to become reordered, it is not possible to continue enumeration without calling the Reset method. Because keys can be inherited and their behavior changed, their absolute uniqueness cannot be guaranteed by comparisons using the Equals method.



Thanks for Your Visit

Google Search
Disclaimer and Copy Right