评论

收藏

[C++] C#合并两个Dictionary的方法

编程语言 编程语言 发布于:2021-07-25 21:56 | 阅读数:402 | 评论:0

直接代码:
DSC0000.gif DSC0001.gif
1  public Dictionary<string, string> MergeDictionary(Dictionary<string, string> first, Dictionary<string, string> second)
 2     {
 3       if (first == null) first = new Dictionary<string, string>();
 4       if (second == null) return first;
 5 
 6       foreach (var item in second)
 7       {
 8          if (!first.ContainsKey(item.Key))
 9            first.Add(item.Key, item.Value);        
10       }
11 
12       return first;
13     }
View Code
第二种:
1  public Dictionary<string, string> MergeDictionary(Dictionary<string, string> first, Dictionary<string, string> second)
 2     {
 3       if (first == null) first = new Dictionary<string, string>();
 4       if (second == null) return first;
 5 
 6       //相对于第一种只是修改了遍历的方法
 7       foreach (string key in second.Keys)
 8       {
 9         if (!first.ContainsKey(key))
10           first.Add(key,second[key]);
11       }
12       return first;
13     }
View Code



关注下面的标签,发现更多相似文章