I've got a generic dictionary Dictionary<string, T>
that I would like to essentially make a Clone() of ..any suggestions.
14 Answers
(Note: although the cloning version is potentially useful, for a simple shallow copy the constructor I mention in the other post is a better option.)
How deep do you want the copy to be, and what version of .NET are you using? I suspect that a LINQ call to ToDictionary, specifying both the key and element selector, will be the easiest way to go if you're using .NET 3.5.
For instance, if you don't mind the value being a shallow clone:
var newDictionary = oldDictionary.ToDictionary(entry => entry.Key,
entry => entry.Value);
If you've already constrained T to implement ICloneable:
var newDictionary = oldDictionary.ToDictionary(entry => entry.Key,
entry => (T) entry.Value.Clone());
(Those are untested, but should work.)
-
Thanks for the answer Jon. I'm actually using v2.0 of the framework. – mikeymo Sep 26 '08 at 14:09
-
What is "entry => entry.Key, entry => entry.Value" in this context. How will i add key and value. It showing an error at my end – Pratik Jul 30 '13 at 04:58
-
2@Pratik: They're lambda expressions - part of C# 3. – Jon Skeet Jul 30 '13 at 05:51
-
2By default LINQ's ToDictionary doesn't copy the comparer. You mentioned copying the comparer in your other answer, but I'm thinking this version of cloning should also pass the comparer. – user420667 Aug 25 '16 at 23:06
Okay, the .NET 2.0 answers:
If you don't need to clone the values, you can use the constructor overload to Dictionary which takes an existing IDictionary. (You can specify the comparer as the existing dictionary's comparer, too.)
If you do need to clone the values, you can use something like this:
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,
original.Comparer);
foreach (KeyValuePair<TKey, TValue> entry in original)
{
ret.Add(entry.Key, (TValue) entry.Value.Clone());
}
return ret;
}
That relies on TValue.Clone()
being a suitably deep clone as well, of course.

- 1,421,763
- 867
- 9,128
- 9,194
-
I think that's only doing a shallow copy of the dictionary values, though. The `entry.Value` value might be yet another [sub]collection. – ChrisW Jan 04 '17 at 15:27
-
6@ChrisW: Well that's asking each value to be cloned - it's up to the `Clone()` method whether it's deep or shallow. I've added a note to that effect. – Jon Skeet Jan 04 '17 at 15:39
-
Dear @JonSkeet, What about the following approach? https://stackoverflow.com/questions/5963115/how-do-i-copy-the-content-of-a-dictionary-to-an-new-dictionary-in-c?answertab=votes#tab-top Which one do yous suggest? – Saeed Ganji Apr 15 '19 at 13:53
-
2@SaeedGanji: Well if the values don't need to be cloned, the "use the constructor overload to Dictionary which takes an existing IDictionary" is fine, and already in my answer. If the values *do* need to be cloned, then the answer you've linked to doesn't help at all. – Jon Skeet Apr 15 '19 at 13:55
-
@JonSkeet So if I keep objects which are structs rather than classes in the dictionary the constructor approach is satisfying, right? – Saeed Ganji Apr 15 '19 at 14:08
-
1@SaeedGanji: That should be fine, yes. (Of course, if the structs contain references to mutable reference types, that could still be a problem... but hopefully that's not the case.) – Jon Skeet Apr 15 '19 at 14:10
-
> "use the constructor overload to Dictionary which takes an existing IDictionary" is fine" < This is also thread-safe, right? I do not require to add a lock object while cloning? – Saeed Ganji Apr 16 '19 at 07:20
-
1@SaeedGanji: That depends on what else is going on. If other threads are only *reading* from the original dictionary, then I believe it should be fine. If anything is modifying it, you'll need to lock in both that thread and the cloning thread, to avoid them happening at the same time. If you want thread safety when using dictionaries, use `ConcurrentDictionary`. – Jon Skeet Apr 16 '19 at 07:25
-
Dear @JonSkeet, thanks for your prompt replies, I am using ConcurrentDictionary, but at some point, I need to Clone its values and clear it, after the clone operation I need to continue on the cloned one, while other threads are writing at high speed on the main CD. I do not want to miss any values – Saeed Ganji Apr 16 '19 at 07:29
-
@SaeedGanji: If you're cloning *from* a ConcurrentDictionary, that should be fine. But at this point I think if you have any further issues you should ask in a new post, rather than adding comments here. – Jon Skeet Apr 16 '19 at 07:34
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191893/discussion-between-saeed-ganji-and-jon-skeet). – Saeed Ganji Apr 16 '19 at 07:43
Dictionary<string, int> dictionary = new Dictionary<string, int>();
Dictionary<string, int> copy = new Dictionary<string, int>(dictionary);

- 2,342
- 1
- 22
- 28
-
9The pointers of the values are still the same, if you apply changes to the values in copy, the changes will also be reflected in the dictionary object. – Fokko Driesprong Apr 25 '12 at 09:21
-
4
-
29This definitely works well - it creates a clone of both the key and value. Of course, this only works if the value is NOT a reference type, if the value is a reference type then it effectively only takes a copy of the keys as its a shallow copy. – Contango May 12 '14 at 13:57
-
1@Contango so in this case since string and int are NOT a reference type it will work right? – Furkan Gözükara Sep 03 '15 at 20:41
-
3@UğurAldanmaz you forget to test an actual change to a referenced object, you only test replacement of value pointers in the cloned dictionaries which obviously works, but your tests will fail if you just change properties on your test objects, like so: https://dotnetfiddle.net/xmPPKr – Jens May 09 '16 at 12:06
-
1
-
it works and this is good that does not clone the referenced objects! We want have a clone of Dictionary not its referenced values. – Omid.Hanjani Dec 15 '18 at 08:17
-
Important gotcha, this does not work for ReadOnlyDictionary, as it just wraps the original dictionary. – Red Riding Hood Oct 08 '21 at 11:52
-
1
That's what helped me, when I was trying to deep copy a Dictionary < string, string >
Dictionary<string, string> dict2 = new Dictionary<string, string>(dict);
Good luck

- 469
- 4
- 2
-
3
-
14This makes a shallow copy. If the value of dict2 is a reference type, then the value of dict is also updated by dict2. (based on .NET5) – help May 24 '22 at 02:55
-
For .NET 2.0 you could implement a class which inherits from Dictionary
and implements ICloneable
.
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;
}
}
You can then clone the dictionary simply by calling the Clone
method. Of course this implementation requires that the value type of the dictionary implements ICloneable
, but otherwise a generic implementation isn't practical at all.

- 11,892
- 2
- 25
- 22
This works fine for me
// assuming this fills the List
List<Dictionary<string, string>> obj = this.getData();
List<Dictionary<string, string>> objCopy = new List<Dictionary<string, string>>(obj);
As Tomer Wolberg describes in the comments, this does not work if the value type is a mutable class.

- 2,281
- 5
- 29
- 43
-
1This seriously needs upvotes ! If the original dictionary is readonly, however, this will still work: var newDict = readonlyDict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) – Stephan Møller Jan 16 '19 at 10:36
-
4
You could always use serialization. You could serialize the object then deserialize it. That will give you a deep copy of the Dictionary and all the items inside of it. Now you can create a deep copy of any object that is marked as [Serializable] without writing any special code.
Here are two methods that will use Binary Serialization. If you use these methods you simply call
object deepcopy = FromBinary(ToBinary(yourDictionary));
public Byte[] ToBinary()
{
MemoryStream ms = null;
Byte[] byteArray = null;
try
{
BinaryFormatter serializer = new BinaryFormatter();
ms = new MemoryStream();
serializer.Serialize(ms, this);
byteArray = ms.ToArray();
}
catch (Exception unexpected)
{
Trace.Fail(unexpected.Message);
throw;
}
finally
{
if (ms != null)
ms.Close();
}
return byteArray;
}
public object FromBinary(Byte[] buffer)
{
MemoryStream ms = null;
object deserializedObject = null;
try
{
BinaryFormatter serializer = new BinaryFormatter();
ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
ms.Position = 0;
deserializedObject = serializer.Deserialize(ms);
}
finally
{
if (ms != null)
ms.Close();
}
return deserializedObject;
}

- 9,840
- 11
- 50
- 71
The best way for me is this:
Dictionary<int, int> copy= new Dictionary<int, int>(yourListOrDictionary);

- 835
- 7
- 8
-
4isnt this just copying the reference and not the values since Dictionary is a reference type? that means if you change the values in one it will change the value in the other? – Goku Nov 24 '18 at 01:03
-
1No, it's creating a `new` Dictionary (note the key word "new") and filling it with values of the old object. For an
it's a perfectly independent copy. – Sinus the Tentacular Apr 28 '21 at 11:54 -
1This makes a shallow copy. If the value of the dictionary is a reference type, only the address value is copied. – help May 24 '22 at 03:16
Binary Serialization method works fine but in my tests it showed to be 10x slower than a non-serialization implementation of clone. Tested it on Dictionary<string , List<double>>

- 48,783
- 32
- 145
- 190

- 41
- 1
-
Are you sure you did a full deep copy? Both the strings and Lists need to be deep copied. There are also some bugs in the serialization version causing it to be slow: in `ToBinary()` the `Serialize()` method is called with `this` instead of `yourDictionary`. Then in `FromBinary()` the byte[] is first copied manually to the MemStream but it can just be supplied to its constructor. – Jupiter Apr 10 '14 at 20:28
Try this if key/values are ICloneable:
public static Dictionary<K,V> CloneDictionary<K,V>(Dictionary<K,V> dict) where K : ICloneable where V : ICloneable
{
Dictionary<K, V> newDict = null;
if (dict != null)
{
// If the key and value are value types, just use copy constructor.
if (((typeof(K).IsValueType || typeof(K) == typeof(string)) &&
(typeof(V).IsValueType) || typeof(V) == typeof(string)))
{
newDict = new Dictionary<K, V>(dict);
}
else // prepare to clone key or value or both
{
newDict = new Dictionary<K, V>();
foreach (KeyValuePair<K, V> kvp in dict)
{
K key;
if (typeof(K).IsValueType || typeof(K) == typeof(string))
{
key = kvp.Key;
}
else
{
key = (K)kvp.Key.Clone();
}
V value;
if (typeof(V).IsValueType || typeof(V) == typeof(string))
{
value = kvp.Value;
}
else
{
value = (V)kvp.Value.Clone();
}
newDict[key] = value;
}
}
}
return newDict;
}

- 93
- 8
In the case you have a Dictionary of "object" and object can be anything like (double, int, ... or ComplexClass):
Dictionary<string, object> dictSrc { get; set; }
public class ComplexClass : ICloneable
{
private Point3D ...;
private Vector3D ....;
[...]
public object Clone()
{
ComplexClass clone = new ComplexClass();
clone = (ComplexClass)this.MemberwiseClone();
return clone;
}
}
dictSrc["toto"] = new ComplexClass()
dictSrc["tata"] = 12.3
...
dictDest = dictSrc.ToDictionary(entry => entry.Key,
entry => ((entry.Value is ICloneable) ? (entry.Value as ICloneable).Clone() : entry.Value) );

- 138
- 1
- 9
Here is some real "true deep copying" without knowing type with some recursive walk, good for the beginnig. It is good for nested types and almost any tricky type I think. I did not added nested arrays handling yet, but you can modify it by your choice.
Dictionary<string, Dictionary<string, dynamic>> buildInfoDict =
new Dictionary<string, Dictionary<string, dynamic>>()
{
{"tag",new Dictionary<string,dynamic>(){
{ "attrName", "tag" },
{ "isCss", "False" },
{ "turnedOn","True" },
{ "tag",null }
} },
{"id",new Dictionary<string,dynamic>(){
{ "attrName", "id" },
{ "isCss", "False" },
{ "turnedOn","True" },
{ "id",null }
} },
{"width",new Dictionary<string,dynamic>(){
{ "attrName", "width" },
{ "isCss", "True" },
{ "turnedOn","True" },
{ "width","20%" }
} },
{"height",new Dictionary<string,dynamic>(){
{ "attrName", "height" },
{ "isCss", "True" },
{ "turnedOn","True" },
{ "height","20%" }
} },
{"text",new Dictionary<string,dynamic>(){
{ "attrName", null },
{ "isCss", "False" },
{ "turnedOn","True" },
{ "text","" }
} },
{"href",new Dictionary<string,dynamic>(){
{ "attrName", null },
{ "isCss", "False" },
{ "flags", "removeAttrIfTurnedOff" },
{ "turnedOn","True" },
{ "href","about:blank" }
} }
};
var cln=clone(buildInfoDict);
public static dynamic clone(dynamic obj)
{
dynamic cloneObj = null;
if (IsAssignableFrom(obj, typeof(IDictionary)))
{
cloneObj = Activator.CreateInstance(obj.GetType());
foreach (var key in obj.Keys)
{
cloneObj[key] = clone(obj[key]);
}
}
else if (IsNumber(obj) || obj.GetType() == typeof(string))
{
cloneObj = obj;
}
else
{
Debugger.Break();
}
return cloneObj;
}
public static bool IsAssignableFrom(this object obj, Type ObjType = null, Type ListType = null, bool HandleBaseTypes = false)
{
if (ObjType == null)
{
ObjType = obj.GetType();
}
bool Res;
do
{
Res = (ObjType.IsGenericType && ObjType.GetGenericTypeDefinition().IsAssignableFrom(ListType)) ||
(ListType == null && ObjType.IsAssignableFrom(obj.GetType()));
ObjType = ObjType.BaseType;
} while ((!Res && ObjType != null) && HandleBaseTypes && ObjType != typeof(object));
return Res;
}
public static bool IsNumber(this object value)
{
return value is sbyte
|| value is byte
|| value is short
|| value is ushort
|| value is int
|| value is uint
|| value is long
|| value is ulong
|| value is float
|| value is double
|| value is decimal;
}

- 11
- 2
Here is another way to clone a dictionary, assuming you know to do the "right" thing as far as handling whatever is hiding behind the "T" (a.k.a. "object") in your specific circumstances.
internal static Dictionary<string, object> Clone(Dictionary<string, object> dictIn)
{
Dictionary<string, object> dictOut = new Dictionary<string, object>();
IDictionaryEnumerator enumMyDictionary = dictIn.GetEnumerator();
while (enumMyDictionary.MoveNext())
{
string strKey = (string)enumMyDictionary.Key;
object oValue = enumMyDictionary.Value;
dictOut.Add(strKey, oValue);
}
return dictOut;
}

- 181
- 2
- 3
I would evaluate if T was a value or reference type. In the case T was a value type I would use the constructor of Dictionary, and in the case when T was a reference type I would make sure T inherited from ICloneable.
It will give
private static IDictionary<string, T> Copy<T>(this IDictionary<string, T> dict)
where T : ICloneable
{
if (typeof(T).IsValueType)
{
return new Dictionary<string, T>(dict);
}
else
{
var copy = new Dictionary<string, T>();
foreach (var pair in dict)
{
copy[pair.Key] = pair.Value;
}
return copy;
}
}

- 189
- 8