Here you should use Contains or TryGetValue.
if (myDic.ContainsKey) { // get value out of dictionary var myValue = myDic["myKey"]; }
or
string myValue; if (myDic.TryGetValue("myKey", out myValue) {
[the example above assumed that your dictionary has type string values, but it could be anything]
The advantage of the second approach is that you can check if an element exists in the dictionary and get its value as a single atomic operation, instead of doing the Contains check first and then a second call to get the value.
source share