IncrementAt<T>

using System.Collections.Generic;

namespace KMorcinek.Common
{
    public static class DictionaryExtensions
    {
        public static void IncrementAt<T>(this Dictionary<T, int> dictionary, T index)
        {
            int count = 0;

            dictionary.TryGetValue(index, out count);

            dictionary[index] = ++count;
        }
    }
}
Example:
var animalQuantities = new Dictionary<string, int>();
animalQuantities.IncrementAt("cat");
animalQuantities.IncrementAt("cat");

Console.WriteLine(animalQuantities["cat"]); // 2

Description

Increment counter at the key passed as argument. Dictionary is <TKey, Int>

Details

Double click on the code to select all.

 

;