<p>There doesn't seem to be a dictionary.AddRange() method. Does anyone know a better way to copy the items to another dictionary without using a foreach loop. </p>

<p>I'm using the System.Collections.Generic.Dictionary. This is for .NET 2.0. </p>

[{'answer_id': 81579, 'author': 'ageektrapped', 'author_id': 631, 'author_profile': 'https://Stackoverflow.com/users/631', 'pm_score': 5, 'selected': False, 'text': "<p>There's the <code>Dictionary</code> constructor that takes another <code>Dictionary</code>.</p>\n\n<p>You'll have to cast it <code>IDictionary</code>, but there is an <code>Add()</code> overload that takes <code>KeyValuePair&lt;TKey, TValue&gt;</code>. You're still using foreach, though.</p>\n"}, {'answer_id': 81615, 'author': 'Oli', 'author_id': 12870, 'author_profile': 'https://Stackoverflow.com/users/12870', 'pm_score': 0, 'selected': False, 'text': '<p>If you\'re dealing with two existing objects, you might get some mileage with the CopyTo method: <a href="http://msdn.microsoft.com/en-us/library/cc645053.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/cc645053.aspx</a></p>\n\n<p>Use the Add method of the other collection (receiver) to absorb them.</p>\n'}, {'answer_id': 81624, 'author': 'Martin Marconcini', 'author_id': 2684, 'author_profile': 'https://Stackoverflow.com/users/2684', 'pm_score': 0, 'selected': False, 'text': "<p>I don't understand, why not using the Dictionary( Dictionary ) (as suggested by ageektrapped ). </p>\n\n<p>Do you want to perform a Shallow Copy or a Deep Copy? (that is, both Dictionaries pointing to the same references or new copies of every object inside the new dictionary?)</p>\n\n<p>If you want to create a <em>new</em> Dictionary pointing to <em>new</em> objects, I think that the only way is through a <strong>foreach</strong>.</p>\n"}, {'answer_id': 81952, 'author': 'Mike Dimmick', 'author_id': 6970, 'author_profile': 'https://Stackoverflow.com/users/6970', 'pm_score': 5, 'selected': True, 'text': "<p>There's nothing wrong with a for/foreach loop. That's all a hypothetical AddRange method would do anyway.</p>\n\n<p>The only extra concern I'd have is with memory allocation behaviour, because adding a large number of entries could cause multiple reallocations and re-hashes. There's no way to increase the capacity of an existing Dictionary by a given amount. You might be better off allocating a new Dictionary with sufficient capacity for both current ones, but you'd still need a loop to load at least one of them.</p>\n"}, {'answer_id': 84308, 'author': 'BFree', 'author_id': 15861, 'author_profile': 'https://Stackoverflow.com/users/15861', 'pm_score': 2, 'selected': False, 'text': '<p>For fun, I created this extension method to dictionary. This should do a deep copy wherever possible.</p>\n<pre><code>public static Dictionary&lt;TKey, TValue&gt; DeepCopy&lt;TKey,TValue&gt;(this Dictionary&lt;TKey, TValue&gt; dictionary)\n        {\n            Dictionary&lt;TKey, TValue&gt; d2 = new Dictionary&lt;TKey, TValue&gt;();\n\n            bool keyIsCloneable = default(TKey) is ICloneable;\n            bool valueIsCloneable = default(TValue) is ICloneable;\n\n            foreach (KeyValuePair&lt;TKey, TValue&gt; kvp in dictionary)\n            {\n                TKey key = default(TKey);\n                TValue value = default(TValue);\n                if (keyIsCloneable)\n                {\n                    key = (TKey)((ICloneable)(kvp.Key)).Clone();\n                }\n\n                else\n                {\n                    key = kvp.Key;\n                }\n\n                if (valueIsCloneable)\n                {\n                    value = (TValue)((ICloneable)(kvp.Value)).Clone();\n                }\n\n                else\n                {\n                    value = kvp.Value;\n                }\n\n                d2.Add(key, value);\n            }\n\n            return d2;\n        }\n</code></pre>\n'}, {'answer_id': 45593250, 'author': 'Mahendra Thorat', 'author_id': 7305904, 'author_profile': 'https://Stackoverflow.com/users/7305904', 'pm_score': 4, 'selected': False, 'text': '<pre><code>var Animal = new Dictionary&lt;string, string&gt;();\n</code></pre>\n\n<p>one can pass existing animal Dictionary to the constructor.</p>\n\n<pre><code>Dictionary&lt;string, string&gt; NewAnimals = new Dictionary&lt;string, string&gt;(Animal);\n</code></pre>\n'}, {'answer_id': 60526164, 'author': 'Marcello', 'author_id': 6366781, 'author_profile': 'https://Stackoverflow.com/users/6366781', 'pm_score': 0, 'selected': False, 'text': '<p>For a primitive type dictionary:</p>\n\n<pre><code>public void runIntDictionary()\n{\n    Dictionary&lt;int, int&gt; myIntegerDict = new Dictionary&lt;int, int&gt;() { { 0, 0 }, { 1, 1 }, { 2, 2 } };\n    Dictionary&lt;int, int&gt; cloneIntegerDict = new Dictionary&lt;int, int&gt;();\n    cloneIntegerDict = myIntegerDict.Select(x =&gt; x.Key).ToList().ToDictionary&lt;int, int&gt;(x =&gt; x, y =&gt; myIntegerDict[y]);\n}\n</code></pre>\n\n<p>or with an Object that implement ICloneable:</p>\n\n<pre><code>public void runObjectDictionary()\n{\n    Dictionary&lt;int, number&gt; myDict = new Dictionary&lt;int, number&gt;() { { 3, new number(3) }, { 4, new number(4) }, { 5, new number(5) } };\n    Dictionary&lt;int, number&gt; cloneDict = new Dictionary&lt;int, number&gt;();\n    cloneDict = myDict.Select(x =&gt; x.Key).ToList().ToDictionary&lt;int, number&gt;(x =&gt; x, y =&gt; myDict[y].Clone());\n}\n\npublic class number : ICloneable\n{\n    public number()\n    {\n    }\n    public number(int newNumber)\n    {\n        nr = newnumber;\n    }\n    public int nr;\n\n    public object Clone()\n    {\n        return new number() { nr = nr };\n    }\n    public override string ToString()\n    {\n        return nr.ToString();\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/81552', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13028/']