public static void AddOrUpdate <TKey, TValue>(this IDictionary <TKey, TValue> @this, params Tuple <TKey, TValue>[] values)
        {
            if (@this == null)
            {
                throw new NullReferenceException();
            }
            Contract.EndContractBlock();

            foreach (var kvp in values)
            {
                @this.AddOrUpdate(kvp.Item1, kvp.Item2);
            }
        }
        public static void AddOrUpdate <TKey, TValue>(this IDictionary <TKey, TValue> @this, params KeyValuePair <TKey, TValue>[] values)
        {
            if (@this == null)
            {
                throw new NullReferenceException();
            }
#if NET40
            Contract.EndContractBlock();
#endif

            foreach (var kvp in values)
            {
                @this.AddOrUpdate(kvp.Key, kvp.Value);
            }
        }
        public static void AddOrUpdate <TKey, TValue>(this IDictionary <TKey, TValue> @this, IEnumerable <Tuple <TKey, TValue> > values)
        {
            if (@this == null)
            {
                throw new NullReferenceException();
            }
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }
            Contract.EndContractBlock();

            foreach (var kvp in values)
            {
                @this.AddOrUpdate(kvp.Item1, kvp.Item2);
            }
        }
        public static void AddOrUpdate <TKey, TValue>(this IDictionary <TKey, TValue> @this, IEnumerable <KeyValuePair <TKey, TValue> > values)
        {
            if (@this == null)
            {
                throw new NullReferenceException();
            }
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }
#if NET40
            Contract.EndContractBlock();
#endif

            foreach (var kvp in values)
            {
                @this.AddOrUpdate(kvp.Key, kvp.Value);
            }
        }
Beispiel #5
0
 /// <summary>
 /// Adds a key/value pair to the <see cref="IDictionary{TKey, TValue}"/> if the key does not already exist, or updates a key/value pair in the <see cref="IDictionary{TKey, TValue}"/> by using the specified function if the key already exists.
 /// </summary>
 /// <param name="dictionary">Dictionary to check and get</param>
 /// <param name="key"> The key to be added or whose value should be updated</param>
 /// <param name="factory">The function used to generate a value to be added for an absent key or updated for an present key.</param>
 /// <typeparam name="TKey">Type of the key</typeparam>
 /// <typeparam name="TValue">Type of the value</typeparam>
 /// <returns>The new value for the key. This will be either be the result of factory.</returns>
 public static TValue AddOrUpdate <TKey, TValue>(this IDictionary <TKey, TValue> dictionary, TKey key, Func <TValue> factory) => dictionary.AddOrUpdate(key, k => factory());