How To Iterate the Hashtable in C#

Hashtable ss = new Hashtable();
ss["key1"] ="india";
ss["key2"] = "bharat";

foreach (object gg in ss)
Console.WriteLine("Key value is " + gg);

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);

A DictionaryEntry object is simply a container containing the Key and Value .
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!