Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 15551

Incrementing a numerical value in a dictionary

$
0
0

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?


Viewing all articles
Browse latest Browse all 15551

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>