C# Combine Dictionary Keys

Set collection

You need to union all the keys in several Dictionaries, yielding a collection of every key in your Dictionaries that you can display. We use the reliable framework HashSet class for the best results. You can combine Dictionary keys in the C# programming language and see what keys are shared.

Key collections:

cat, dog         [1]
cat, man         [2]
sheep, fish, cat [3]

Union:

cat, dog, man, sheep, fish

Venn diagram

First, the visual aid here is a Venn diagram, which shows what we want to combine: the string keys in each circle. However, there are duplicates, which we want to eliminate. Next, you can see the textual format of the Venn diagram, which more clearly expresses the required union values.

Venn diagram of Dictionary keys

Example

Here we look at how you can combine Dictionaries with the above keys using HashSet. The solution uses HashSet's UnionWith instance method. This combines the HashSet with another collection of the same value type. This is a complete console application written in the C# programming language that you can run in Visual Studio.

HashSet Programs

This C# example shows how to combine (union) all the keys in several Dictionary instances.

Program that uses HashSet [C#]

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	// A. First dictionary.
	Dictionary<string, int> d1 = new Dictionary<string, int>();
	d1.Add("cat", 1);
	d1.Add("dog", 2);

	// B. Second dictionary.
	Dictionary<string, int> d2 = new Dictionary<string, int>();
	d2.Add("cat", 3);
	d2.Add("man", 4);

	// C. Third dictionary.
	Dictionary<string, int> d3 = new Dictionary<string, int>();
	d3.Add("sheep", 5);
	d3.Add("fish", 6);
	d3.Add("cat", 7);

	// D. Get union of all keys.
	HashSet<string> h1 = new HashSet<string>(d1.Keys);
	h1.UnionWith(d2.Keys);
	h1.UnionWith(d3.Keys);

	// E. Display all the keys.
	// You can look up the values in the loop body.
	foreach (string k in h1)
	{
	    Console.WriteLine(k);
	}
    }
}

Output

cat
dog
man
sheep
fish

First three steps in example. These parts of the above code simply populate dictionaries with the strings we are using. The approach in this article would work on Dictionaries with any value type.

Part D in the example. What happens in D is that a new HashSet is created from the Keys collection of type string. This is then UnionWith'ed on the other two Dictionary Keys properties.

Read MSDN

MSDN provides some examples of HashSet use, although they are a bit harder to follow. Here's the description of UnionWith: "Modifies the current HashSet(T) object to contain all elements that are present in both itself and in the specified collection."

MSDN reference

Notes

Note (please read)

This code is fairly fast, but the performance hasn't been extensively tested. What the code does is quite clear. If you didn't use HashSet, you would have to write more code to combine all the keys. You could refactor the HashSet code into a method. You can use the "Extract Method" command in Visual Studio 2008.

Possible usages. In my application, I had three separate Dictionaries, which were built from three different sources. I needed them to be separate. However, I wanted to display them all together in an ASP.NET webpage displayed as HTML. The HashSet code was ideal.

Summary

The C# programming language

Here we looked at how you can use HashSet with Dictionary keys to combine them and find the union of the keys. HashSet contains the ideal logic for combining Dictionary keys. In school, our professors teach us about unions and sets, but most of us forget. However, HashSet comes to our rescue with its excellent and fast UnionWith method.

Collections
.NET