Coder Perfect

What is the most efficient method for cloning/deep copying a.NET generic Dictionarystring, T>?


I’ve got a generic dictionary Dictionarystring, T> that I’d like to Clone()…any ideas?

Asked by mikeymo

Solution #1

(Note: while the cloning version is possibly useful, the constructor I mentioned in the previous post is a better alternative for a simple shallow copy.)

What version of.NET are you using, and how deep do you want the copy to go? If you’re using.NET 3.5, I think a LINQ call to ToDictionary with both the key and element selector will be the quickest way to go.

For example, if you don’t mind being a superficial clone, you could:

var newDictionary = oldDictionary.ToDictionary(entry => entry.Key,
                                               entry => entry.Value);

If T has already been forced to adopt ICloneable:

var newDictionary = oldDictionary.ToDictionary(entry => entry.Key, 
                                               entry => (T) entry.Value.Clone());

(These haven’t been tried, but they should work.)

Answered by Jon Skeet

Solution #2

The.NET 2.0 responses are as follows:

You can use the constructor overload to Dictionary, which takes an existing IDictionary, if you don’t need to clone the values. (Alternatively, you can use the comparer from an existing dictionary.)

You can use something like this to clone the values if you need to:

public static Dictionary<TKey, TValue> CloneDictionaryCloningValues<TKey, TValue>
   (Dictionary<TKey, TValue> original) where TValue : ICloneable
    Dictionary<TKey, TValue> ret = new Dictionary<TKey, TValue>(original.Count,
    foreach (KeyValuePair<TKey, TValue> entry in original)
        ret.Add(entry.Key, (TValue) entry.Value.Clone());
    return ret;

TValue is necessary for this. Of course, Clone() is also a sufficiently deep clone.

Answered by Jon Skeet

Solution #3

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

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

Answered by Herald Smit

Solution #4

When I was trying to deep copy a Dictionary string, string >, that’s what came in handy.

Dictionary<string, string> dict2 = new Dictionary<string, string>(dict);

Good luck

Answered by peter feldman

Solution #5

You may create a class that inherits from Dictionary and implements ICloneable for.NET 2.0.

public class CloneableDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TValue : ICloneable
    public IDictionary<TKey, TValue> Clone()
        CloneableDictionary<TKey, TValue> clone = new CloneableDictionary<TKey, TValue>();

        foreach (KeyValuePair<TKey, TValue> pair in this)
            clone.Add(pair.Key, (TValue)pair.Value.Clone());

        return clone;

The dictionary can then be cloned by simply calling the Clone function. Of course, this implementation needs that the dictionary’s value type implements ICloneable, but a general implementation isn’t feasible otherwise.

Answered by Compile This

Post is based on