How do I copy the content of a dictionary to an new dictionary in C#?

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)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)