1

I have to copy one dictionary, work with that copy and return to the original one.

What seems to happen is that the orignal dictionary is modified when I do some work on the copied one.

Here is my code :

dmodified_profile = new SortedDictionary<int,SortedDictionary<string,List<string>>>(d_profile);

I don't know why d_profile which is the original one could be modified if my modifications are done on the dmodified_profile dictionary ?

Thanks

Bernard Larouche
  • 1,211
  • 2
  • 13
  • 22
  • 3
    You might consider investigating using an immutable data structure. They are highly amenable to the sort of operation you're talking about. – Eric Lippert Dec 17 '09 at 23:59

2 Answers2

5

You need to deep copy.

Community
  • 1
  • 1
Alex Brasetvik
  • 11,218
  • 2
  • 35
  • 36
3

Your SortedDictionary maps an integer to a REFERENCE to another SortedDictionary. When you copy that dictionary you copy the values of the keys as well as the REFERENCEs of the values, because your dictionary's value is of a reference type.

Andre Pena
  • 56,650
  • 48
  • 196
  • 243