结合两个键的字典的C#程序
首先,设置要组合的字典-
Dictionary <string, int> dict1 = new Dictionary <string, int> ();
dict1.Add("one", 1);
dict1.Add("Two", 2);
Dictionary <string, int> dict2 = new Dictionary <string, int> ();
dict2.Add("Three", 3);
dict2.Add("Four", 4);现在,使用HashSet组合它们。用于相同目的的方法是UnionWith()-
HashSet <string> hSet = new HashSet <string> (dict1.Keys); hSet.UnionWith(dict2.Keys);
以下是完整的代码-
示例
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
Dictionary <string, int> dict1 = new Dictionary <string, int> ();
dict1.Add("one", 1);
dict1.Add("Two", 2);
Dictionary <string, int> dict2 = new Dictionary <string, int> ();
dict2.Add("Three", 3);
dict2.Add("Four", 4);
HashSet <string> hSet = new HashSet <string> (dict1.Keys);
hSet.UnionWith(dict2.Keys);
Console.WriteLine("Union of Dictionary...");
foreach(string val in hSet) {
Console.WriteLine(val);
}
}
}输出结果
Union of Dictionary... one Two Three Four
热门推荐
10 换工作简短特别祝福语
11 女孩自己生日祝福语简短
12 送同学的简短祝福语
13 默克尔离职祝福语简短
14 冬至祝福语简短走心
15 女士祝福语幽默简短英语
16 最佳元旦祝福语简短学校
17 新婚祝福语英语简短精辟
18 客户去旅游祝福语简短