C# Copy Dictionary

Dictionary illustration

You need to copy all the keys and values inside one Dictionary into another Dictionary. There is a Dictionary constructor that can do this. Review this method and look at the difference between Dictionary references and data.

Example

First, you will find that the simplest way of copying your Dictionary, using a custom loop over the keys, is prone to code duplication and errors. It doesn't make any sense to write the loop yourself, when that exact same code is present in the Dictionary class itself. Here we see how you can use the copy constructor, and the example demonstrates how the internal structures are copied.

This C# program copies all the elements in a Dictionary into another.

Program that copies entire Dictionary [C#]

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	//
	// Create and initialize Dictionary.
	//
	Dictionary<string, int> dictionary = new Dictionary<string, int>();
	dictionary.Add("cat", 1);
	dictionary.Add("dog", 3);
	dictionary.Add("iguana", 5);

	//
	// Copy the Dictionary to a second object.
	//
	Dictionary<string, int> copy = new Dictionary<string, int>(dictionary);

	//
	// Change the first Dictionary. It won't change the copy.
	//
	dictionary.Add("fish", 4);

	//
	// Display the first Dictionary.
	//
	Console.WriteLine("--- Dictionary 1 ---");
	foreach (var pair in dictionary)
	{
	    Console.WriteLine(pair);
	}
	//
	// Display the second Dictionary.
	//
	Console.WriteLine("--- Dictionary 2 ---");
	foreach (var pair in copy)
	{
	    Console.WriteLine(pair);
	}
    }
}

Output
    (Modifying the first Dictionary doesn't affect the copied Dictionary.)

--- Dictionary 1 ---
[cat, 1]
[dog, 3]
[iguana, 5]
[fish, 4]
--- Dictionary 2 ---
[cat, 1]
[dog, 3]
[iguana, 5]
Main method

Notes. The code defines the Main entry point, and initializes a new Dictionary first. It populates the Dictionary with three key/value pairs. Next, it copies the Dictionary into the second Dictionary 'copy'. Finally, it adds a key to the first Dictionary, and displays both Dictionaries. The key added after the copy was made is not in the copy.

Internals

.NET Framework information

As we noted, you can copy all elements in your Dictionary manually. Internally, the Dictionary constructor that accepts IDictionary is implemented with this loop. My research on Dictionary loops established that the foreach loop is very fast and eliminates lookups.

Implementation of Dictionary copy constructor

foreach (KeyValuePair<TKey, TValue> pair in dictionary)
{
    this.Add(pair.Key, pair.Value);
}

Notes. You could easily adapt the above loop to copy to a Hashtable or List of KeyValuePair structs. There may be more compact methods to copy to other structures, however.

KeyValuePair Hints

Summary

The C# programming language

Here we saw how you can copy an entire Dictionary to a new Dictionary. The Dictionary class does not define a Copy or CopyTo method, so using the copy constructor is the easiest and clearest way. Finally, we saw the internals of this constructor, which you can adapt to other collections if required.

Collections
.NET