Dictionary append question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csharpula csharp

    Dictionary append question

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

    Dictionary<stri ng,objectdic1 and Dictionary<stri ng,objectdic2 into
    one new Dictionary<stri ng,object?

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

    Thank you very much!



    *** Sent via Developersdex http://www.developersdex.com ***
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Dictionary append question

    On May 19, 8:43 am, csharpula csharp <csharp...@yaho o.comwrote:
    Hello,
    I was wondering if there is a way in c# to append two Dictionaries of
    the same type:
    >
    Dictionary<stri ng,objectdic1  and Dictionary<stri ng,objectdic2 into
    one new Dictionary<stri ng,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

    Comment

    • Marc Gravell

      #3
      Re: Dictionary append question

      You could simply create one such a method though.

      And in C#3, an extension method (perhaps on IDictionary<TKe y,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

      Comment

      • csharpula csharp

        #4
        Re: Dictionary append question

        Could you please explain how can I create a general method for
        Dictionary (tkey,tvalue) merge?



        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • Marc Gravell

          #5
          Re: Dictionary append question

          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(r ight) - you have to do SomeClass.AddRa nge(left,right)
          [perhaps renaming the method].

          Marc

          using System;
          using System.Collecti ons.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(r ight);
          }
          }

          public static class DictionaryExt
          {
          public static void AddRange<TKey, TValue>(
          this IDictionary<TKe y, TValuedestinati on,
          IDictionary<TKe y, TValuevalues)
          {
          if (destination == null) throw new
          ArgumentNullExc eption("destina tion");
          if (values == null) throw new ArgumentNullExc eption("values" );

          foreach (var pair in values)
          {
          destination.Add (pair);
          }
          }
          public static void Merge<TKey, TValue>(
          this IDictionary<TKe y, TValuedestinati on,
          IDictionary<TKe y, TValuevalues)
          {
          if (destination == null) throw new
          ArgumentNullExc eption("destina tion");
          if (values == null) throw new ArgumentNullExc eption("values" );

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

          Comment

          Working...