473,246 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Dictionary append question

Hello,
I was wondering if there is a way in c# to append two Dictionaries of
the same type:

Dictionary<string,objectdic1 and Dictionary<string,objectdic2 into
one new Dictionary<string,object?

(Is there any kind of AddRange such as exisits in generic list)?

Thank you very much!

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
4 9894
On May 19, 8:43*am, csharpula csharp <csharp...@yahoo.comwrote:
Hello,
I was wondering if there is a way in c# to append two Dictionaries of
the same type:

Dictionary<string,objectdic1 *and Dictionary<string,objectdic2 into
one new Dictionary<string,object?

(Is there any kind of AddRange such as exisits in generic list)?
It's not that simple, what happens if both dics share a common key?

You could simply create one such a method though. You could even use a
delegate to defer the decision of what to do with the shared keys
Jun 27 '08 #2
You could simply create one such a method though.

And in C#3, an extension method (perhaps on IDictionary<TKey,TValue>)
would be trivial. But as Ignacio rightly says - you need to decide how
to treat conflicts. Personally I'd probably just let it blow up by
default...

Marc
Jun 27 '08 #3
Could you please explain how can I create a general method for
Dictionary (tkey,tvalue) merge?

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #4
Something like below; this specific example uses C# 3, but you can do
similar with C# 2 - you just lose the ability to use
left.AddRange(right) - you have to do SomeClass.AddRange(left,right)
[perhaps renaming the method].

Marc

using System;
using System.Collections.Generic;
using System.Xml.Linq;
static class Program
{
static void Main()
{
// dictionary
var left = new Dictionary<int, string>();
left.Add(1, "Fred");
left.Add(2, "Barney");

var right = new Dictionary<int, string>();
right.Add(3, "Wilma");
right.Add(4, "Betty");

left.AddRange(right);
}
}

public static class DictionaryExt
{
public static void AddRange<TKey, TValue>(
this IDictionary<TKey, TValuedestination,
IDictionary<TKey, TValuevalues)
{
if (destination == null) throw new
ArgumentNullException("destination");
if (values == null) throw new ArgumentNullException("values");

foreach (var pair in values)
{
destination.Add(pair);
}
}
public static void Merge<TKey, TValue>(
this IDictionary<TKey, TValuedestination,
IDictionary<TKey, TValuevalues)
{
if (destination == null) throw new
ArgumentNullException("destination");
if (values == null) throw new ArgumentNullException("values");

foreach (var pair in values)
{
destination[pair.Key] = pair.Value;
}
}
}
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Quentin Crain | last post by:
Hello again All! (First, I would like to mention I did try to google for the answer here!) Say I am populating a dictionary with a list and appending. I have written it thusly: d={} for...
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
3
by: Roland Rickborn | last post by:
Hi folks, I am relatively new to Python. Although I read a lot of howtos, introductions and wikis, I am still having trouble ;-) My querstion: As the subject says, I'd like to feed a...
7
by: Brian Elmegaard | last post by:
Hi I have written the following which works, but I would like to write it less clumsy. I have a dictionary in which I loop through the keys for a dynamic programming algorithm. If a key is...
10
by: Ben | last post by:
Hello... I have set up a dictionary into whose values I am putting a list. I loop around and around filling my list each time with new values, then dumping this list into the dictionary. Or so I...
6
by: Tina I | last post by:
Hi everyone, I have a small, probably trivial even, problem. I have the following HTML: I need to make this into a dictionary like this: dictionary = {"METAR:" : "ENBR 270920Z 00000KT 9999...
4
by: =?utf-8?B?TWFjaWVqIEJsaXppxYRza2k=?= | last post by:
Hi Pythonistas! I've got a question about storing tuples in a dictionary. First, a small test case which creates a list of dictionaries: import time list_of_dicts = keys = prev_clk =...
11
by: Dan Upton | last post by:
This might be more information than necessary, but it's the best way I can think of to describe the question without being too vague. The task: I have a list of processes (well, strings to...
0
by: Gary Herron | last post by:
Dan Upton wrote: Yes. Create a list of keys, and loop through it: pids = procs_dict.keys() for pid in pids: if procs_dict.poll() != None # do the counter updates del procs_dict Then the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.