using System; using System.Collections.Generic; public class Test { public static void Main() { var c = new Counter(); c.Increment(1); c.Increment(2); c.Increment(1); c.Increment(1); foreach (var kvp in c) { Console.WriteLine(kvp); } } public class Counter : IEnumerable> { private Dictionary counts = new Dictionary(); public void Increment(T key) { int current; bool exists = counts.TryGetValue(key, out current); if (exists) { counts[key]++; } else { counts[key] = 1; } } IEnumerator> IEnumerable>.GetEnumerator() { return ((IEnumerable>) counts).GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return counts.GetEnumerator(); } } }