【问题标题】:SortedDictionary add one SortedDictionary into anotherSortedDictionary 将一个 SortedDictionary 添加到另一个
【发布时间】:2010-04-05 03:13:09
【问题描述】:

我有一个现有的SortedDictionary<string, int> 的要求。现在我正在创建一个不同的 SortedDictionary 并希望将其添加到第一个中。怎么做?

【问题讨论】:

  • 第二个也是 SortedDictionary 还是 SortedDictionary>?

标签: c# .net sorteddictionary


【解决方案1】:

只需将它传递给构造函数:

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

【讨论】:

    【解决方案2】:

    SortedDictionary&lt;TKey,TValue&gt; 不提供AddRange(IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;) 函数,因此您必须以一种艰难的方式完成它,一次一项。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      相关资源
      最近更新 更多