fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. public static void Main() {
  8. var dict = new Dictionary<string,List<int>>();
  9. dict.Insert("x", 1);
  10. dict.Insert("x", 2);
  11. dict.Insert("x", 3);
  12. dict.Insert("y", 4);
  13. dict.Insert("y", 5);
  14. foreach (var p in dict) {
  15. Console.WriteLine("{0} - {1}", p.Key, string.Join(", ", p.Value.ToArray()));
  16. }
  17. }
  18. }
  19.  
  20. static class DictExtensions {
  21. public static void Insert<TKey,TVal>(this IDictionary<TKey,List<TVal>> d, TKey k, TVal v) {
  22. List<TVal> current;
  23. if (!d.TryGetValue(k, out current)) {
  24. d.Add(k, new List<TVal> { v } );
  25. } else {
  26. current.Add(v);
  27. }
  28. }
  29. }
Success #stdin #stdout 0.02s 15076KB
stdin
Standard input is empty
stdout
x - 1, 2, 3
y - 4, 5