Wednesday 15 February 2012

Getting value from HashTable

Hi Big,

I have faced this problem many times I junked into the logic. Getting Value/Key from the HashTable is needed every time we work with two values simultaneously. So I thought to write it here. There are two way to do this one is from DictionaryEntry collection and through IDictionaryEnumerator.


HashTable objHash=new HashTable();

objHash.Add(1,"AAA");
objHash.Add(2,"BBB");

1:Getting through DictionaryEntry:

foreach (DictionaryEntry entry in objHash)
            {
                Console.WriteLine(entry.Key.ToString()+","+entry.Value.ToString());
            }

 2:Getting through IDictionaryEnumerator:

            IDictionaryEnumerator objDictionary = objHash.GetEnumerator();

            while (objDictionary.MoveNext())
            {
                Console.WriteLine(objDictionary.Key.ToString()+","+objDictionary.Value.ToString());
            }

No comments:

Post a Comment