471,348 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 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 9802
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

125 posts views Thread by Raymond Hettinger | last post: by
3 posts views Thread by Roland Rickborn | last post: by
7 posts views Thread by Brian Elmegaard | last post: by
10 posts views Thread by Ben | last post: by
6 posts views Thread by Tina I | last post: by
4 posts views Thread by =?utf-8?B?TWFjaWVqIEJsaXppxYRza2k=?= | last post: by
11 posts views Thread by Dan Upton | last post: by
reply views Thread by Gary Herron | last post: by
reply views Thread by Ronak mishra | last post: by

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.