2013-03-04 60 views
1

我hastable入住HashTable集合中是否存在鍵/值對

Hashtable hash = new Hashtable(); 
hash.Add("a", "1"); 
hash.Add("b","2"); 
hash.Add("c","3"); 
hash.Add("c","4" 

現在,我需要檢查重點=「c」和值=「3」組合已經存在於哈希表與否。

hash.ContainsKey值功能cheks天氣密鑰是否存在和ContainsValue功能檢查天氣值是否存在或不存在。但是,如果我試圖

if(hash.Contains("c") && hash.ContainsValue("3")) 
{ 
    // some code heree 
} 

比它會爲這兩個 「C,3」 和 「c,4」 combinathion返回true。

我需要檢查鍵/值對組合如何檢查?

+1

無論天氣好不好,天氣不好,天氣是否寒冷,天氣是否炎熱,無論天氣如何,無論我們喜不喜歡,天氣都是天氣。 (SCNR) – Corak 2013-03-04 10:06:41

+0

真的嗎?你可以添加一個重複的密鑰到散列表?!哈希表是一個非通用字典,據我所知,所有字典拒絕重複鍵被添加... – stt106 2015-01-28 12:25:21

回答

7
if(hash.ContainsKey("c") && hash["c"] == "3") { } 
2

您可以檢查密鑰是否存在&然後檢查相應密鑰的值。

if(hash.ContainsKey("key") && hash["key"] == "3") 
{ 
    // contains key and value 
} 
相關問題