检查哈希表集合中是否存在键值对

本文关键字:存在 键值对 是否 哈希表 集合 检查 | 更新日期: 2023-09-27 18:01:58

我有一个稳定的

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

现在我需要检查Key = "c"和value= "3"组合是否已经存在于哈希表中。

hash.ContainsKey值函数检查天气键是否存在,ContainsValue函数检查天气值是否存在。但如果我尝试

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

则对于"c,3"answers"c,4"组合都将返回true。

我需要检查键/值对组合如何检查?

检查哈希表集合中是否存在键值对

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

可以检查key是否存在&然后检查对应键的值。

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