Add contents of a Dictionary to another Dictionary in C#
This post will discuss how to add the contents of a Dictionary to another Dictionary in C#. The solution should add key-value pairs present in the given dictionary into the source dictionary.
1. Using List<T>.ForEach() method
The idea is to convert the second dictionary into a List of KeyValuePair<K,V> Then, insert each entry into the first dictionary using the ForEach() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static void Append<K, V>(this Dictionary<K, V> first, Dictionary<K, V> second) { List<KeyValuePair<K, V>> pairs = second.ToList(); pairs.ForEach(pair => first.Add(pair.Key, pair.Value)); } } public class Example { public static void Main() { Dictionary<string, string> first = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; Dictionary<string, string> second = new Dictionary<string, string> { { "key3", "value3" }, { "key4", "value4" } }; first.Append(second); Console.WriteLine(String.Join(Environment.NewLine, first)); } } /* Output: [key1, value1] [key2, value2] [key3, value3] [key4, value4] */ |
The above code throws an ArgumentException when attempting to add a duplicate key.
To overwrite the value of a key already present in the dictionary, we can use dict[key] = value syntax instead of the Add() method. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static void Append<K, V>(this Dictionary<K, V> first, Dictionary<K, V> second) { second.ToList() .ForEach(pair => first[pair.Key] = pair.Value); } } public class Example { public static void Main() { Dictionary<string, string> first = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; Dictionary<string, string> second = new Dictionary<string, string> { { "key2", "value" }, { "key3", "value3" } }; first.Append(second); Console.WriteLine(String.Join(Environment.NewLine, first)); } } /* Output: [key1, value1] [key2, value] [key3, value3] */ |
2. Using foreach loop
We can also iterate over the dictionary using a regular foreach loop and append all the second dictionary entries into the first dictionary. This solution handles duplicate keys as it uses the dict[key] property instead of the Add() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using System; using System.Collections.Generic; public static class Extensions { public static void Append<K, V>(this Dictionary<K, V> first, Dictionary<K, V> second) { foreach (KeyValuePair<K, V> item in second) { first[item.Key] = item.Value; } } } public class Example { public static void Main() { Dictionary<string, string> first = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; Dictionary<string, string> second = new Dictionary<string, string> { { "key2", "value" }, { "key3", "value3" } }; first.Append(second); Console.WriteLine(String.Join(Environment.NewLine, first)); } } /* Output: [key1, value1] [key2, value] [key3, value3] */ |
That’s all about adding contents of a Dictionary to another Dictionary in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)