-2

I have the following method that makes a deep copy of a dictionary:

public static Dictionary<string, MyClass> deepCopyDic(Dictionary<string, MyClass> src)
{
    //Copies a dictionary with all of its elements
    //RETURN:
    //      = Dictionary copy
    Dictionary<string, MyClass> dic = new Dictionary<string, MyClass>();
    for (int i = 0; i < src.Count; i++)
    {
        dic.Add(src.ElementAt(i).Key, new MyClass(src.ElementAt(i).Value));
    }

    return dic;
}

I was wondering, can I somehow make it into a template? I need MyClass to be a template.

3
  • ... a template for what? Apr 11, 2013 at 18:41
  • @JeremyHolovacs: For MyClass to be MyOtherClass or MyAnotherClass, etc.
    – ahmd0
    Apr 11, 2013 at 18:42
  • you mean an abstract class? An interface? Apr 11, 2013 at 18:46

3 Answers 3

6

You can use Generics with where TValue : ICloneable constraint:

public static Dictionary<TKey, TValue> deepCopyDic<TKey, TValue>(Dictionary<TKey, TValue> src)
    where TValue : ICloneable
{
    //Copies a dictionary with all of its elements
    //RETURN:
    //      = Dictionary copy
    Dictionary<TKey, TValue> dic = new Dictionary<TKey, TValue>();
    foreach (var item in src)
    {
        dic.Add(item.Key, (TValue)item.Value.Clone());
    }

    return dic;
}

You'll have to implement ICloneable interface in every class you'd like to pass into that method.

Or a bit improved version, with Key cloned as well:

public static Dictionary<TKey, TValue> deepCopyDic<TKey, TValue>(Dictionary<TKey, TValue> src)
    where TValue : ICloneable
    where TKey : ICloneable
{
    return src.ToDictionary(i => (TKey)i.Key.Clone(), i => (TValue)i.Value.Clone());
}
29
  • Thanks. That's what I was missing :)
    – ahmd0
    Apr 11, 2013 at 18:45
  • So instead of ICloneable can I just copy it with the new and the constructor?
    – ahmd0
    Apr 11, 2013 at 18:47
  • 1
    @ahmd0 And I'm telling you that your code to iterate the dictionary is very broken, even before you do anything with it. Next, a copy isn't just deep or shallow, it's more complex than that. It's a sliding scale. You could copy a reference to the dictionary. You could create a new dictionary but just copy the references to items in it. You could create a new dictionary and new items, but copy references of items inside those items, etc. A "deep copy" implies going all the way to the lowest level, not stopping at some earlier point.
    – Servy
    Apr 11, 2013 at 20:52
  • 1
    @ahmd0 only to leave me hanging? After so many answers/comments i still don't know what you are really want to do. Don't you think I may be a problem with you. Or what do you understand from copying a dictionary?.
    – I4V
    Apr 11, 2013 at 22:04
  • 1
    @ahmd0 If only there was some place where you could go to get information about virtually anything, let's call this made up place "Google". Going to such a place might allow you to enter a word, say "serialization", perhaps with some additional context, say "serialization C#" and instantly get millions of references to detailed and reputable sources describing what the concept was, and how you could go about using it. Wouldn't the mythical world containing such a marvelous tool be wonderful.
    – Servy
    Apr 11, 2013 at 22:19
2

You could use the copy constructor option:

Dictionary<string, int> copy = new Dictionary<string, int>(dictionary);

This way you make a deep copy of your dictionary. Link to the original post.

1

The Serialized approach is the only way as noted above. ICloneable does not guarantee that all properties in the object being clone is not assigning references unless you have full control over the object which is never a good assumption, especially in a large team.

The only cavet of the Serialized approach is that all objects being passed in the dictionary are serializable. Also, serializing is not always very efficient because of the over use of Reflection that occurs, which shouldn't be used in high preformance areas of code.

I solved this problem using an approach known as fast serialization but it requires that all objects that you plan to clone support a specific interface. It's always speed vs. complexity.

1
  • Oh, I forgot to mention. Dictionaries are not Serializable. So, if you plan to use the Serializable technique, you would need to iterate all your objects in the Dictionary.
    – code5
    Apr 11, 2013 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.