Hey I have following code like this
public object RetrieveItemRun(int item)
{
if (dictionary.ContainsKey(item))
{
MessageBox.Show("Retrieving" + item.ToString());
}
return dictionary[item];
}
It always crashes when trying to get key of 0, the message box does show so the ContainsKey method is true, but when I try to retrieve the value from the key it crashes saying:
“The given key was not present in the dictionary”
You are trying to retrieve the key independent on if it exists. Try by changing the code to:
If exists, then return the item. You original code returns assuming item exits (outside check)