Merging dictionaries in C#

前端 未结 26 1301
温柔的废话
温柔的废话 2020-11-22 08:25

What\'s the best way to merge 2 or more dictionaries (Dictionary) in C#? (3.0 features like LINQ are fine).

I\'m thinking of a method signa

26条回答
  •  迷失自我
    2020-11-22 08:58

    public static IDictionary AddRange(this IDictionary one, IDictionary two)
            {
                foreach (var kvp in two)
                {
                    if (one.ContainsKey(kvp.Key))
                        one[kvp.Key] = two[kvp.Key];
                    else
                        one.Add(kvp.Key, kvp.Value);
                }
                return one;
            }
    

提交回复
热议问题