44

How can I copy a Dictionary<string, string> to another new Dictionary<string, string> so that they are not the same object?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
hansgiller
  • 973
  • 4
  • 12
  • 15

3 Answers3

110

Assuming you mean you want them to be individual objects, and not references to the same object pass the source dictionary into the destination's constructor:

Dictionary<string, string> d = new Dictionary<string, string>();
Dictionary<string, string> d2 = new Dictionary<string, string>(d);

"so that they are not the same object."

Ambiguity abound - if you do actually want them to be references to the same object:

Dictionary<string, string> d = new Dictionary<string, string>();
Dictionary<string, string> d2 = d;

(Changing either d or d2 after the above will affect both)

idbrii
  • 10,975
  • 5
  • 66
  • 107
Jaymz
  • 6,140
  • 2
  • 26
  • 30
  • 6
    Just as a side note, something that tripped me up once. If you use this method to copy a static dictionary, then changes made in the copy will still effect the original – stuicidle Jun 30 '17 at 10:25
  • 2
    another approach could be found https://stackoverflow.com/questions/139592/what-is-the-best-way-to-clone-deep-copy-a-net-generic-dictionarystring-t?answertab=votes#tab-top – Saeed Ganji Apr 15 '19 at 14:19
11
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, string> first = new Dictionary<string, string>()
        {
            {"1", "One"},
            {"2", "Two"},
            {"3", "Three"},
            {"4", "Four"},
            {"5", "Five"},
            {"6", "Six"},
            {"7", "Seven"},
            {"8", "Eight"},
            {"9", "Nine"},
            {"0", "Zero"}
        };

        Dictionary<string, string> second = new Dictionary<string, string>();
        foreach (string key in first.Keys)
        {
            second.Add(key, first[key]);
        }

        first["1"] = "newone";
        Console.WriteLine(second["1"]);
    }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Amal Hashim
  • 501
  • 3
  • 13
8

A one-line version of Amal's answer:

var second = first.Keys.ToDictionary(_ => _, _ => first[_]);
Zodman
  • 3,178
  • 2
  • 32
  • 38
  • 2
    Curious: Why didn't you do this instead? `var second = first.ToDictionary(kvp => k.Key, kvp => kvp.Value);` – Granger Sep 30 '21 at 16:36
  • When performance is not critical and I'm faced with different possibilities for syntax, I go for the version I believe is easier to read. I believe explicitly using `first.Keys.ToDictionary(_` reads better than `first.ToDictionary(kvp=> k.Key` because it reads more fluently and the variable names are not required to describe intent (hence the use of the underscore/discard). So mine is __the Keys of `first` as a dictionary__, then I look at the last parameter and see the values are just the values by key. Your syntax with discards works too: `first.ToDictionary(_ => _.Key, _ => _.Value)` – Zodman Oct 02 '21 at 09:23