I have a scenario where there is a dictionary that might or might not have a key value at a given time. I am presently testing to see if the value exists in the following manner
Take a look at the dictionary's TryGetValue method
int myInt;
if (!_myDictionary.TryGetValue(key, out myInt))
{
myInt = 0;
}
A couple of people have suggested using ContainsKey. This is not a good idea if you actually want the value because it will mean 2 lookups - e.g.
if (_myDictionary.ContainsKey(key)) // look up 1
{
myInt = _myDictionary[key]; // look up 2
}