WPF_常用字典扩展方法

来源:互联网 发布:电力监控软件 编辑:程序博客网 时间:2024/04/29 21:28
    public static class DictionaryExtensions    {        public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Action<TKey, TValue> toAction)        {            foreach (var pair in dictionary)            {                toAction(pair.Key, pair.Value);            }        }        public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Action<TKey, TValue, int> toAction)        {            int i = 0;            foreach (var pair in dictionary)            {                toAction(pair.Key, pair.Value, i++);            }        }        public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)        {            TValue value;            return dictionary.TryGetValue(key, out value) ? value : default(TValue);        }        public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defalutValue)        {            TValue value;            return dictionary.TryGetValue(key, out value) ? value : defalutValue;        }        public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> defalutValueFactory)        {            TValue value;            return dictionary.TryGetValue(key, out value) ? value : defalutValueFactory(key);        }        public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)        {            return dictionary.GetOrAdd(key, x => value);        }        public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> addValueFactory)        {            TValue value;            if (!dictionary.TryGetValue(key, out value))            {                value = addValueFactory(key);                dictionary.Add(key, value);            }            return value;        }        public static void AddOrUpdate<TKey1, TKey2, TValue>(            this Dictionary<TKey1, Dictionary<TKey2, TValue>> dictionary, TKey1 key1, TKey2 key2, TValue value)        {            Dictionary<TKey2, TValue> dictionary2;            if (!dictionary.TryGetValue(key1, out dictionary2))            {                dictionary2 = new Dictionary<TKey2, TValue>();                dictionary.Add(key1, dictionary2);            }            dictionary2[key2] = value;        }        public static TValue AddOrUpdate<TKey, TValue>(            this Dictionary<TKey, TValue> dictionary,            TKey key,            Func<TKey, TValue> addValueFactory,            Func<TKey, TValue, TValue> updateValueFactory)        {            TValue value;            if (dictionary.TryGetValue(key, out value))            {                value = updateValueFactory(key, value);                dictionary[key] = value;            }            else            {                value = addValueFactory(key);                dictionary.Add(key, value);            }            return value;        }        public static void AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, IEnumerable<KeyValuePair<TKey, TValue>> kvps)        {            var collection = (ICollection<KeyValuePair<TKey, TValue>>)dictionary;            kvps.ForEach(kvp => collection.Add(kvp));        }        public static void AddOrUpdateRange<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, IEnumerable<KeyValuePair<TKey, TValue>> kvps)        {            kvps.ForEach(pair => dictionary[pair.Key] = pair.Value);        }        public static bool TryGetValue<TKey1, TKey2, TValue>(            this Dictionary<TKey1, Dictionary<TKey2, TValue>> dictionary, TKey1 key1, TKey2 key2, out TValue value)        {            Dictionary<TKey2, TValue> dictionary2;            if (dictionary.TryGetValue(key1, out dictionary2))                return dictionary2.TryGetValue(key2, out value);            value = default(TValue);            return false;        }    }

0 0