Since each element of the hashtable is of IDictionary key/value pair type. Hashtable can be enumerated through DictionaryEntry structure in System.Collections namespace in c#. DictionaryEntry class defines a dictionary key/value pair that can be set or retrieved.
The following sample would help in hashtable elements enumeration.
//create a new hashtable
System.Collections.Hashtable hsh = new System.Collections.Hashtable();
//add some elements in the hashtable
hsh.Add("1", "london dreams review");
hsh.Add("2", "karisma");
hsh.Add("3", "the band movie");
hsh.Add("4", "end of days");
//enumerate or looping through all the elements in the hashtable
foreach (System.Collections.DictionaryEntry entry in hsh)
{
Console.WriteLine("Key: " + entry.Key.ToString() + " | " + "Value: " + entry.Value.ToString());
}
Unless you are still using .net 1, I would suggest you investigate the Dictionary class which is a strongly typed version of the HashTable. It allows you to do IDictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "foo"); dictionary.Add(2, "bar"); The advantages with it are that firstly you get design time type safety, it will not allow you to add ("3", "foobar") because it is expecting an int as the key in the example above. Also you are not boxing everything that you add to an object.