I'm using the code below to either increment or insert a value in a dictionary. If the key I'm incrementing doesn't exist I'd like to set its value to 1.
public void IncrementCount(Dictionary<int, int> someDictionary, int id) { int currentCount; if (someDictionary.TryGetValue(id, out currentCount)) { someDictionary[id] = currentCount + 1; } else { someDictionary[id] = 1; } }
Is this an appropriate way of doing so?