Retrieve Key from Value in Hash Table

16. December 2009 02:10

C# Code Snippets  1 Comment

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;
Currently rated 4.2 by 11 people

Comments (1)

Michael Rodrigues Michael Rodrigues
2/4/2016 5:35:28 PM #

Great, simple and working, saved me a lot of time. Thanks for sharing.

Add Comment

Visit to discover Indian blogs Computers Blogs