Generally when we use the collections in C# , we use the object for iteration.
Example
ArrayList list = new ArrayList(); list.Add("india"); list.Add("bharat"); foreach (object gg in list) { Console.WriteLine("Value is " + gg); Console.Read(); }
But in case of Hashtable it is little bit different.
If we write in the same way as above for the hashtable then we would not get the desired values.
Hashtable ss = new Hashtable(); ss["key1"] ="india"; ss["key2"] = "bharat"; foreach (object gg in ss) { Console.WriteLine("Key value is " + gg); Console.Read(); }
Here we get System.Collections.DictionaryEntry and System.Collections.DictionaryEntry as output instead of the value pairs stored in the hashtable.
In this case we can help of DictinaryEntry object for iterating the hashtable.
foreach(DictionaryEntry gg in ss) { Console.WriteLine("Key and value are " + gg.Key + " " + gg.Value); Console.Read(); }
A DictionaryEntry object is simply a container containing the Key and Value .