Check whether key/value pair exists in hashtable collection

情到浓时终转凉″ 提交于 2019-12-19 10:18:21

问题


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 ?


回答1:


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



回答2:


You can check if key exists & then check for the value of the corresponding key.

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


来源:https://stackoverflow.com/questions/15198718/check-whether-key-value-pair-exists-in-hashtable-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!