Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

Dictionary append question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
csharpula csharp - 19 May 2008 13:43 GMT
Hello,
I was wondering if there is a way in c# to append two Dictionaries of
the same type:

Dictionary<string,object> dic1  and Dictionary<string,object> dic2 into
one new Dictionary<string,object> ?

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

Thank you very much!
Ignacio Machin ( .NET/ C# MVP ) - 19 May 2008 14:16 GMT
> Hello,
> I was wondering if there is a way in c# to append two Dictionaries of
[quoted text clipped - 4 lines]
>
> (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
Marc Gravell - 19 May 2008 14:23 GMT
> 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
csharpula csharp - 19 May 2008 14:57 GMT
Could you please explain how can I create a general method for
Dictionary (tkey,tvalue) merge?
Marc Gravell - 19 May 2008 20:05 GMT
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, TValue> destination,
       IDictionary<TKey, TValue> values)
   {
       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, TValue> destination,
       IDictionary<TKey, TValue> values)
   {
       if (destination == null) throw new
ArgumentNullException("destination");
       if (values == null) throw new ArgumentNullException("values");

       foreach (var pair in values)
       {
           destination[pair.Key] = pair.Value;
       }
   }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.