views:

1397

answers:

5

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.

I'm using the System.Collections.Generic.Dictionary. This is for .NET 2.0.

+6  A: 

There's the Dictionary constructor that takes another Dictionary.

You'll have to cast it IDictionary, but there is an Add() overload that takes KeyValuePair. You're still using foreach, though.

ageektrapped
Thanks for the answer, but both dictionaries have items in them at the point I need to do the copy.
Helephant
A: 

If you're dealing with two existing objects, you might get some mileage with the CopyTo method: http://msdn.microsoft.com/en-us/library/cc645053.aspx

Use the Add method of the other collection (receiver) to absorb them.

Oli
I couldn't find an Add method for Dictionary that would take an Array?
Helephant
A: 

I don't understand, why not using the Dictionary( Dictionary ) (as suggested by ageektrapped ).

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?)

If you want to create a new Dictionary pointing to new objects, I think that the only way is through a foreach.

Martín Marconcini
Shallow copy is fine. I have two dictionaries that I'm populating in a method and I want to copy the second smaller dictionary into the first one at the end of the method. I need to keep them separate during the life of the method because they mean different things.
Helephant
+3  A: 

There's nothing wrong with a for/foreach loop. That's all a hypothetical AddRange method would do anyway.

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.

Mike Dimmick
+1  A: 

For fun, I created this extension method to dictionary. This should do a deep copy wherever possible.

public static Dictionary<TKey, TValue> DeepCopy<TKey,TValue>(this Dictionary&lt;TKey, TValue> dictionary)
        {
            Dictionary<TKey, TValue> d2 = new Dictionary<TKey, TValue>();

            bool keyIsCloneable = default(TKey) is ICloneable;
            bool valueIsCloneable = default(TValue) is ICloneable;

            foreach (KeyValuePair<TKey, TValue> kvp in dictionary)
            {
                TKey key = default(TKey);
                TValue value = default(TValue);
                if (keyIsCloneable)
                {
                    key = (TKey)((ICloneable)(kvp.Key)).Clone();
                }

                else
                {
                    key = kvp.Key;
                }

                if (valueIsCloneable)
                {
                    value = (TValue)((ICloneable)(kvp.Value)).Clone();
                }

                else
                {
                    value = kvp.Value;
                }

                d2.Add(key, value);
            }

            return d2;
        }
BFree