Add a collection of items into a dictionary, using a collection of keys and one default value for all the items - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

Add a collection of items into a dictionary, using a collection of keys and one default value for all the items

Demo Code


using System.Text;
using System.Collections.Generic;
using System.Collections;
using System;//from   w ww .j a v  a 2  s  .c  om

public class Main{
        /// <summary>
      /// Add a collection of items into a dictionary, using a collection of keys and one default value for all the items
      /// </summary>
      /// <param name="original">The original.</param>
      /// <param name="newKeys">The new keys.</param>
      /// <param name="defaultValue">The default value.</param>
      public static void DictionaryAddRange(IDictionary original, IEnumerable newKeys, object defaultValue) {
         foreach (object key in newKeys) {
            if (original.Contains(key))
               throw new NotSupportedException("Two dictionaries have overlap keys");
            original.Add(key, defaultValue);
         }
      }
        /// <summary>
      /// Adds all items from one dictionary to another 
      /// </summary>
      /// <param name="original">The original.</param>
      /// <param name="toAdd">To add.</param>
      public static void DictionaryAddRange(IDictionary original, IDictionary toAdd) {
         foreach (object key in toAdd.Keys) {
            if (original.Contains(key))
               throw new NotSupportedException("Two dictionaries have overlap keys");
            original.Add(key, toAdd[key]);
         }
      }
}

Related Tutorials