C# – What’s the fastest way to copy the values and keys from one dictionary into another in C#

ccopydictionaryforeach

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.

Best Answer

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<TKey, TValue>. You're still using foreach, though.