码迷,mamicode.com
首页 > Windows程序 > 详细

C#合并两个Dictionary的方法

时间:2015-01-13 12:13:21      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:

直接代码:

技术分享
 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

 

C#合并两个Dictionary的方法

标签:

原文地址:http://www.cnblogs.com/tommy-huang/p/4220615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!