combine two maps in csharp

In C#, you can combine two maps or dictionaries using the Merge method or Linq's Concat method. Here are examples using both methods:

main.cs
// Using Merge method:
var map1 = new Dictionary<string, int>()
{
    {"a", 1},
    {"b", 2}
};

var map2 = new Dictionary<string, int>()
{
    {"c", 3},
    {"d", 4}
};

var mergedMap = map1.Merge(map2);

// Using Concat method:
var map3 = new Dictionary<string, int>()
{
    {"a", 5},
    {"e", 6}
};

var concatenatedMap = map1.Concat(map3);

346 chars
25 lines

Note that if there are duplicate keys in the two maps, the value of the key in the second map or dictionary will be used.

gistlibby LogSnag