SortedDictionary 将一个 SortedDictionary 添加到另一个中

发布于 2024-08-27 20:08:48 字数 142 浏览 4 评论 0 原文

我有一个要求,即我已经有一个现有的 SortedDictionary。现在我正在创建一个不同的 SortedDictionary 并希望将其添加到第一个中。怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的笑 2024-09-03 20:08:48

只需将其传递给构造函数即可:

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

Just pass it to the constructor:

var copy = new SortedDictionary<string, int>(original);
心如狂蝶 2024-09-03 20:08:48

SortedDictionary 不提供 AddRange(IEnumerable>) 函数,因此您必须努力做到这一点方式,一次一项。

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

foreach (KeyValuePair<string, int> kvp in second) {
  first.Add(kvp.Key, kvp.Value);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文