How to retrieve value from key

Hi all,

I had a hashtable ht

and I had added

ht.Add("key1","value1");

May I know how to retrieve value1 based on key1

Thanks



Answer this question

How to retrieve value from key

  • Savindra

    The next step is to grab all of the values in a hashtable. This should help.

    foreach(DictionaryEntry _d in ht)

    {

    MessageBox.Show(_d.Key + "-" + _d.Value);

    }


  • PrashanthBlog

    To get the value, use:

    ht["key1"]


  • redcodes

    If you are using .NET 2.0 or later consider using Dictionary<> generic instead of Hashtable as it will be strongly typed. That elliminates the need to cast values as Hashtable works with object.

    Dictionary<string, string> dict = new Dictionary<string, string>();
    dict.Add("key1", "value1");
    string value = dict["key1"];

    foreach (string key in dict.Keys)
    {
       value = dict[key];
    }



  • How to retrieve value from key