Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Append Dictionary onto an existing one

Posted on 2010-09-08
Medium Priority
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-
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 53

Accepted Solution

Dhaest earned 1200 total points
ID: 33626924
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 400 total points
ID: 33627063
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 400 total points
ID: 33627200

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

LVL 12

Expert Comment

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

Author Comment

ID: 33627609
Ok thanks all

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 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