0

The orderedDictionary instantiation is this:

IOrderedDictionary orderedDictionary= gridview.DataKeys[index].Values;

orderedDictionary is read only.

How can I make a deep copy of orderedDictionary that is not read only? Serialization/deserialization doesn't work cause it also copies the read only part.

Răzvan Flavius Panda
  • 21,730
  • 17
  • 111
  • 169
  • Rather than reprise a deepness discussion I provide a link to an old question http://stackoverflow.com/q/78536/659190 – Jodrell Jul 08 '11 at 09:22

1 Answers1

2

The easiest way would be to just copy the objects:

var newDictionary = new OrderedDictionary();
foreach(DictionaryEntry de in orderedDictionary)
{
    newDictionary.Add(de.Key, de.Value);
}

UPDATE:
This code will NOT create a deep copy of the values in the dictionary.
Example:

var orderedDictionary = new OrderedDictionary();
orderedDictionary.Add("1", new List<int> { 1, 2 });

var newDictionary = new OrderedDictionary();
foreach(DictionaryEntry de in orderedDictionary)
{
    newDictionary.Add(de.Key, de.Value);
}

Both dictionary will contain one entry with the key "1" and the same list. Removing an item from this list in any of the dictionaries will also change the contents of the list in the other dictionary, because there only IS one list.

Console.WriteLine(((List<int>)orderedDictionary["1"]).Count);
Console.WriteLine(((List<int>)newDictionary["1"]).Count);
Console.WriteLine(ReferenceEquals(orderedDictionary["1"], newDictionary["1"]));
((List<int>)orderedDictionary["1"]).Remove(1);
Console.WriteLine(((List<int>)orderedDictionary["1"]).Count);
Console.WriteLine(((List<int>)newDictionary["1"]).Count);

This will output the following:

2
2
True
1
1

Assigning a new value to a key in one of the dictionary however has no effect on the other dictionary:

newDictionary["1"] = new List<int>{3,4};
Console.WriteLine(ReferenceEquals(orderedDictionary["1"], newDictionary["1"]));
Console.WriteLine(((List<int>)orderedDictionary["1"]).Count);
Console.WriteLine(((List<int>)newDictionary["1"]).Count);

This will output:

False
2
3
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Just what I was thinking but, would this just copy the references from one dictionary to the other? Would that be "deep"? – Jodrell Jul 08 '11 at 08:49
  • 1
    @Jodrell: You are right, this isn't a deep copy. Although the OP asks about doing a deep copy, I don't think he really wants to do that. I read his question like so, that he just wants to create a new `OrderedDictionary` that is not read only. I may be wrong, though. – Daniel Hilgarth Jul 08 '11 at 08:55
  • @Daniel Hilgarth: Your solution is good, and it does a deep copy of orderedDictionary, i tested it, modifying the newDictionary values doesn't affect the orderedDicionary values. Thanks! – Răzvan Flavius Panda Jul 08 '11 at 09:02
  • 1
    @Jodrell, @Daniel Higarth, seems there are several depths in the deeps. :-) – Jodrell Jul 08 '11 at 09:19
  • @Răzvan: This isn't a real deep copy. Modifying the properties of an item in one of the dictionaries will result in a change of the properties of the same item in the other dictionary, because they really are the same. In other words: You have two different dictionaries, but the objects in those dictionaries are the same. – Daniel Hilgarth Jul 08 '11 at 10:03
  • @Daniel Hilgarth: "i tested it, modifying the newDictionary values doesn't affect the orderedDicionary values." - previous comment, so your solution actually makes new proprieties - it doesn't reference the same ones – Răzvan Flavius Panda Jul 08 '11 at 10:12
  • @Răzvan: Well, that's just not true. I updated my answer, please read it carefully. – Daniel Hilgarth Jul 08 '11 at 10:31
  • @Daniel Hilgarth: Yes, you are right, your code doesn't work if the values are reference type. I tested my code on an OrderedDictionary that has keys and values as strings and it works, dunno why since string is a refernce type but it acts like a value type. – Răzvan Flavius Panda Jul 08 '11 at 11:03
  • @Răzvan: string is immutable, that's why. – Daniel Hilgarth Jul 08 '11 at 11:06
  • @Daniel Hilgarth: The solution you gave me was good for what i needed so i will just change the name of the question. Thank you – Răzvan Flavius Panda Jul 08 '11 at 11:11
  • @Răzvan: You are welcome. I just wanted to make sure, you understood the implications correctly of using my code, so you don't introduce subtle bugs in your application. – Daniel Hilgarth Jul 08 '11 at 11:15