2
using System.Collections.Generic;
3
using ServiceStack.Common.Extensions;
5
namespace ServiceStack.Common
7
public static class DictionaryExtensions
9
public static TValue GetValueOrDefault<TValue, TKey>(this Dictionary<TKey, TValue> dictionary, TKey key)
11
return dictionary.ContainsKey(key) ? dictionary[key] : default(TValue);
14
public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> onEachFn)
16
foreach (var entry in dictionary)
18
onEachFn(entry.Key, entry.Value);
22
public static bool EquivalentTo<K, V>(this IDictionary<K, V> thisMap, IDictionary<K, V> otherMap)
24
if (thisMap == null || otherMap == null) return thisMap == otherMap;
25
if (thisMap.Count != otherMap.Count) return false;
27
foreach (var entry in thisMap)
30
if (!otherMap.TryGetValue(entry.Key, out otherValue)) return false;
31
if (!Equals(entry.Value, otherValue)) return false;
37
public static List<T> ConvertAll<T, K, V>(IDictionary<K, V> map, Func<K, V, T> createFn)
39
var list = new List<T>();
40
map.ForEach((kvp) => list.Add(createFn(kvp.Key, kvp.Value)));
44
public static V GetOrAdd<K, V>(this Dictionary<K, V> map, K key, Func<K,V> createFn)
46
//simulate ConcurrentDictionary.GetOrAdd
50
if (!map.TryGetValue(key, out val))
51
map[key] = val = createFn(key);
b'\\ No newline at end of file'