當前位置:成語大全網 - 新華字典 - c#的hashtable怎麽由值取其對應的鍵值?

c#的hashtable怎麽由值取其對應的鍵值?

我寫的壹個小實例:

Hashtable ht = new Hashtable();

ht.Add("ht1", "111");

ht.Add("ht2", "222");

ht.Add("ht3", "333");

//定義壹個Hashtable

string key = null;

foreach (System.Collections.DictionaryEntry de in ht)

{

if (de.Value.ToString() == "222")

{

key=de.Key.ToString();//得到key

break;//退出foreach遍歷

}

}

Response.Write(key);//輸出ht2