vb.net 如何检查字典是否包含给定值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31472471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a Dictionary contains a given value?
提问by Amal
How do I check if a value exists in a Dictionary(Of int, String)?
如何检查值是否存在于 a 中Dictionary(Of int, String)?
Let's say I have [{1, 'One'};{2, 'Two'};{3, 'Three'}], how to check if ‘Two' exists ?
假设我有[{1, 'One'};{2, 'Two'};{3, 'Three'}],如何检查“两个”是否存在?
回答by raed
You can use ContainsValue:
您可以使用ContainsValue:
If myDictionary.ContainsValue("Two") Then
debug.print("Exists")
End If
That's all you need.
这就是你所需要的。
回答by cyberponk
Complementing raed′s answer, you can also use ContainsKeyto search for the keys instead of the values.
补充 raed 的答案,您还可以使用ContainsKey来搜索键而不是值。
If myDictionary.ContainsKey(1) Then
debug.print("Exists")
End If
This also works with stringkeys, like in the example:
这也适用于字符串键,如示例中所示:
[{"Chris", "Alive"};{"John", "Deceased"}]
If myDictionary.ContainsKey("Chris") Then
debug.print("Chris Exists in dictionary")
End If
If myDictionary.ContainsValue("Alive") Then
debug.print("There is someone alive in the dictionary")
End If

