Retrieve Key from Value in Hash Table

Working with hash tables is pretty simple but few days back I was having a problem in retrieving a key from a value in hash table. I was bit lazy to find a way myself, so I searched the net and here is what I got….a simple piece of code which lead to me to complete my task and so I thought I should share it with everyone here.

public string FindKey(string Value, Hashtable HT)
{
       string Key = “”;
       IDictionaryEnumerator e = HT.GetEnumerator();
       while (e.MoveNext())
       {
            if (e.Value.ToString().Equals(Value))
            {
               Key = e.Key.ToString();
            }
       }
       return Key;
}

comments powered by Disqus