DictionaryHelper

 

 /// <summary>
    /// DictionaryHelper
    /// </summary>
    public static class DictionaryHelper
    {
        /// <summary>
        /// Put 扩展字典方法 存在时更改,不存在时添加
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dictionary"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Dictionary<TKey, TValue> Put<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
        {
            if (dictionary.ContainsKey(key))
            {
                dictionary[key] = value;
            }
            else
            {
                dictionary.Add(key, value);
            }

            return dictionary;
        }

        /// <summary>
        /// Put 扩展字典方法 存在时更改,不存在时添加
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dictionary"></param>
        /// <param name="keyValuePair"></param>
        /// <returns></returns>
        public static Dictionary<TKey, TValue> Put<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, KeyValuePair<TKey, TValue> keyValuePair)
        {
            return dictionary.Put(keyValuePair.Key, keyValuePair.Value);
        }
    }

 

posted @ 2017-06-06 14:05  zslm___  阅读(321)  评论(0编辑  收藏  举报