Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- someDictionary.AddOrUpdate(id, 1, (id, count) => count + 1);
- public void IncrementCount(Dictionary<int, int> someDictionary, int id)
- {
- if (!someDictionary.ContainsKey(id))
- someDictionary[id] = 0;
- someDictionary[id]++;
- }
- int currentCount;
- // currentCount will be zero if the key id doesn't exist..
- someDictionary.TryGetValue(id, out currentCount);
- someDictionary[id] = currentCount + 1;
- public static void Increment<T>(this Dictionary<T, int> dictionary, T key)
- {
- int count;
- dictionary.TryGetValue(key, out count);
- dictionary[key] = count + 1;
- }
- var dictionary = new Dictionary<string, int>();
- dictionary.Increment("hello");
- dictionary.Increment("hello");
- dictionary.Increment("world");
- Assert.AreEqual(2, dictionary["hello"]);
- Assert.AreEqual(1, dictionary["world"]);
Add Comment
Please, Sign In to add comment