Append Dictionary onto an existing one

Posted on 2010-09-08
Last Modified: 2013-12-17

I have an existing dictionary variable:

      Dictionary<string, string> itemList = new Dictionary<string, string> ();

And I would like to add the below one to it so the values are added to the end, its returned from a method.

       Dictionary<string, string> returnValue = new Dictionary<string, string> ();

ie somthing like:  itemList +=returnValue (I know this is wrong)

Question by:darren-w-
LVL 53

Accepted Solution

Dhaest earned 300 total points
Comment Utility
You can the concat

Dictionary<string, string> d3 = d1.Concat(d2).ToDictionary(e => e.Key, e => e.Value);
        foreach (var item in d3)

Assisted Solution

LlamaJoe earned 100 total points
Comment Utility
using LINQ you can join into a new variable, and then assign it back to the original variable.

                    Dictionary<string, string> itemList = new Dictionary<string, string> ();
                    itemList.Add("a", "b");
                    Dictionary<string, string> returnValue = new Dictionary<string, string> ();
                    returnValue.Add("c", "d");

                    var newList = itemList.Concat(returnValue);

Dictionary is not a sealed class, so you could override the Add method to make a method to allow you to add another dictionary and return the result.
LVL 12

Assisted Solution

w00te earned 100 total points
Comment Utility

d1.Concat(d2).ToDictionary(x => x.Key, x => x.Value);

LVL 12

Expert Comment

Comment Utility
Actually mine's prety much the same as Dhaest, go for his.  Sorry about that :)
LVL 13

Author Comment

Comment Utility
Ok thanks all

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now