Tuesday, 25 February 2014

Concatenate Dictionary objects in c#

Dictionary<string, object> d1 = new Dictionary<string, object>();
d1.Add("Name","Talha");
Dictionary<string, object> d2 = new Dictionary<string, object>();
d2.Add("City","Lahore");

// --- Now Concatenate Both Dictionaries using concat Extension method
d1 = d1.Concat(d2).ToDictionary(x=> x.Key , x=> x.Value);

Note:  Here "Concat" is an Extension method  so You have to include it’s Namespace reference on top.

i.e.
using System.Linq;

No comments:

Post a Comment