views:

82

answers:

2

I have a requirement where I already have an existing SortedDictionary<string, int>. Now I am creating a different SortedDictionary and like to add this in the first one . How to do it?

+4  A: 

Just pass it to the constructor:

var copy = new SortedDictionary<string, int>(original);
Hans Passant
A: 

SortedDictionary<TKey,TValue> doesn't provide an AddRange(IEnumerable<KeyValuePair<TKey, TValue>>) function, so you'll have to do it the hard way, one item at a time.

SortedDictionary<string, int> first, second;
first = FillFirst();
second = FillSecond();

foreach (KeyValuePair<string, int> kvp in second) {
  first.Add(kvp.Key, kvp.Value);
}
Ben Voigt
reason for downvote?
Ben Voigt