Something like below; this specific example uses C# 3, but you can do
similar with C# 2 - you just lose the ability to use
left.AddRange(right) - you have to do SomeClass.AddRange(left,right)
[perhaps renaming the method].
Marc
using System;
using System.Collections.Generic;
using System.Xml.Linq;
static class Program
{
static void Main()
{
// dictionary
var left = new Dictionary<int, string>();
left.Add(1, "Fred");
left.Add(2, "Barney");
var right = new Dictionary<int, string>();
right.Add(3, "Wilma");
right.Add(4, "Betty");
left.AddRange(right);
}
}
public static class DictionaryExt
{
public static void AddRange<TKey, TValue>(
this IDictionary<TKey, TValue> destination,
IDictionary<TKey, TValue> values)
{
if (destination == null) throw new
ArgumentNullException("destination");
if (values == null) throw new ArgumentNullException("values");
foreach (var pair in values)
{
destination.Add(pair);
}
}
public static void Merge<TKey, TValue>(
this IDictionary<TKey, TValue> destination,
IDictionary<TKey, TValue> values)
{
if (destination == null) throw new
ArgumentNullException("destination");
if (values == null) throw new ArgumentNullException("values");
foreach (var pair in values)
{
destination[pair.Key] = pair.Value;
}
}
}