2

I have hastable

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

Now I need to check Key = "c" and value= "3" combination is already exits in hashtable or not.

hash.ContainsKey value function cheks weather key is exists or not and ContainsValue function checks weather value is exists or not. But if I tried

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

than it will return true for both "c,3" and "c,4" combinathion.

I need to check key/value pair combination how can I check that ?

4

2 回答 2

9
if(hash.ContainsKey("c") && hash["c"] == "3") { }
于 2013-03-04T09:55:13.997 回答
3

您可以检查键是否存在,然后检查相应键的值。

if(hash.ContainsKey("key") && hash["key"] == "3")
{
    // contains key and value
}
于 2013-03-04T09:55:41.087 回答