开发者

SortedDictionary add one SortedDictionary into another

开发者 https://www.devze.com 2022-12-26 04:28 出处:网络
I have a requirement where I already have an existing SortedDictionary<string, int>. Now I am creating a different SortedD开发者_开发问答ictionary and like to add this in the first one . How to

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


Just pass it to the constructor:

var copy = new SortedDictionary<string, int>(original);


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消