How to combine dictionary keys in C#

1 Answer

0 votes
using System.Collections.Generic;
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, int> dic1 = new Dictionary<string, int>();
            dic1.Add("aaa", 1);
            dic1.Add("bbb", 2);

            Dictionary<string, int> dic2 = new Dictionary<string, int>();
            dic2.Add("ccc", 3);
            dic2.Add("ddd", 4);
            dic2.Add("eee", 5);

            Dictionary<string, int> dic3 = new Dictionary<string, int>();
            dic3.Add("fff", 6);
            dic3.Add("ggg", 7);
            dic3.Add("hhh", 8);
            dic3.Add("iii", 9);

            HashSet<string> hashset = new HashSet<string>(dic1.Keys);
            hashset.UnionWith(dic2.Keys);
            hashset.UnionWith(dic3.Keys);

            foreach (string s in hashset)
                Console.WriteLine(s);
        }
    }
}


/*
run:

aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
iii

*/

 



answered Mar 6, 2017 by avibootz
...