Part 2 Meanwhile "blue" is not found in the Dictionary, so ContainsKey returns False.
Part 3 Here we use ContainsValue, which searches the values in the Dictionary and determines whether the argument is present as a value.
Dim colors = New Dictionary(Of String, Integer)
' Part 1: look up value that exists with ContainsKey.
Console.WriteLine("tan was found!")
' Part 2: look up missing key.
' Not reached.
Console.WriteLine("blue was not found...")
' Part 3: use ContainsValue.
Console.WriteLine("value 200 was found!")
End Moduletan was found!
blue was not found...
value 200 was found!
Performance. The Dictionary in .NET is highly-optimized for key lookup, so ContainsKey will be fast. But values are not hashed, so they cannot be searched quickly.
Warning Avoid using ContainsValue in code that must be fast. Consider creating a separate Dictionary where the values are keys.
Summary. Many programs written in VB.NET that use the Dictionary collection will use ContainsKey. Sometimes, using TryGetValue can be better if avoids a second lookup.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.