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

前端 未结 3 651
温柔的废话
温柔的废话 2021-01-01 09:33

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

3条回答
  •  鱼传尺愫
    2021-01-01 10:21

    Assuming you mean you want them to be individual objects, and not references to the same object:

    Dictionary d = new Dictionary();
    Dictionary d2 = new Dictionary(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 d = new Dictionary();
    Dictionary d2 = d;
    

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

提交回复
热议问题