Combine IDictionary - CSharp System.Collections

CSharp examples for System.Collections:IDictionary

Description

Combine IDictionary

Demo Code


using System.Collections.Generic;
using System;/*from   w w  w.j  a  v  a 2  s .  co m*/

public class Main{
    public static T Combine<T, X> (params T[] collections) where T : class, ICollection<X>, new()
      {
         var r = new T ();
         foreach (var i in collections) {
            r.AddAll (i);
         }
         return r;
      }
    public static IDictionary<TKey, TValue> Combine<TKey, TValue> (params IDictionary<TKey, TValue> [] dicts)
      {
         var r = new Dictionary<TKey, TValue> ();
         foreach (var i in dicts) {
            r.AddAll (i);
         }
         return r;
      }
    public static void AddAll<T> (this ICollection<T> me, IEnumerable<T> other)
      {
         foreach (var i in other) {
            me.Add (i);
         }
      }
    public static void AddAll<TKey, TValue> (this IDictionary<TKey, TValue> me, IDictionary<TKey, TValue> other)
      {
         foreach (var i in other) {
            me [i.Key] = i.Value;
         }
      }
}

Related Tutorials