DictionaryExtensions.cs 935 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace MECF.Framework.Common.Extens
  7. {
  8. public static class DictionaryExtensions
  9. {
  10. public static void TryAddValue<TKey, TValue>(this Dictionary<TKey, List<TValue>> dictionary, TKey key, TValue value)
  11. {
  12. if (dictionary.ContainsKey(key))
  13. {
  14. dictionary[key].Add(value);
  15. }
  16. else
  17. {
  18. dictionary.Add(key, new List<TValue> { value });
  19. }
  20. }
  21. public static void TryAddValue<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
  22. {
  23. if (dictionary.ContainsKey(key))
  24. {
  25. dictionary[key] = value;
  26. }
  27. else
  28. {
  29. dictionary.Add(key, value);
  30. }
  31. }
  32. }
  33. }