I have a helper that merges two or more IDictionary<TKey, TValue>
objects into one IDictionary<TKey, string>
by concatenating the TValue
's ToString()
methods like so:
public class DictionaryHelper<TKey, TValue>
{
public static IDictionary<TKey, string> MergeDictionaries<TKey, TValue>(params IDictionary<TKey, TValue>[] dictionaries) where TValue : class
{
var returnValue = new Dictionary<TKey, string>();
foreach (var dictionary in dictionaries)
{
foreach (var kvp in dictionary)
{
if (returnValue.ContainsKey(kvp.Key))
{
returnValue[kvp.Key] += kvp.Value.ToString();
}
else
{
returnValue[kvp.Key] = kvp.Value.ToString();
}
}
}
return returnValue;
}
}
While this is straightforward and pretty easy to read, it seems like there should be a more efficient way to do this. Is there?