问题
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