WPF_常用字典扩展方法

    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;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值