You could use ToDictionary directly:
public static Dictionary<T, T> MergeDict<T, T>(Dictionary<T, T> a, Dictionary<T, T> b)
{
return a.Concat(b).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
Be aware that this will raise an exception in the case of duplicate keys.
If you need to handle duplicate keys, you'll need to decide how you want this to be handled. For example, this will remove duplicates from "b":
public static Dictionary<T, T> MergeDict<T, T>(Dictionary<T, T> a, Dictionary<T, T> b)
{
return a.Concat(b.Where(kvp => !a.ContainsKey(kvp.Key)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
To get the same behavior as the original, you could go the other way (keeps KeyValuePair values from "b"):
public static Dictionary<T, T> MergeDict<T, T>(Dictionary<T, T> a, Dictionary<T, T> b)
{
return b.Concat(a.Where(kvp => !b.ContainsKey(kvp.Key)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}