You can use the Dictionary.ContainsKey method.
So you should write:
if (myDictionary.ContainsKey("Key2")) {
Other alternatives are to either wrap access in a try...catch , or use TryGetValue (see the examples on the MSDN page associated with).
string result = null; if (dict.TryGetValue("Key2", out result)) {
TryGetMethod more efficient if you want to do something with the result, since you do not need a second call to get the value (as with the ContainsKey method).
(Of course, in both methods you would replace "Key2" with a variable.)
Chrisf
source share