Dictionary.ForEach method










3















I wanna declare new extension method which similar to List.ForEach Method.



What I wanna archive:



var dict = new Dictionary<string, string>()

"K1", "V1" ,
"K2", "V2" ,
"K3", "V3" ,
;


dict.ForEach((x, y) =>

Console.WriteLine($"(Key: x, value: y)");
);


How can I do that?










share|improve this question


























    3















    I wanna declare new extension method which similar to List.ForEach Method.



    What I wanna archive:



    var dict = new Dictionary<string, string>()

    "K1", "V1" ,
    "K2", "V2" ,
    "K3", "V3" ,
    ;


    dict.ForEach((x, y) =>

    Console.WriteLine($"(Key: x, value: y)");
    );


    How can I do that?










    share|improve this question
























      3












      3








      3








      I wanna declare new extension method which similar to List.ForEach Method.



      What I wanna archive:



      var dict = new Dictionary<string, string>()

      "K1", "V1" ,
      "K2", "V2" ,
      "K3", "V3" ,
      ;


      dict.ForEach((x, y) =>

      Console.WriteLine($"(Key: x, value: y)");
      );


      How can I do that?










      share|improve this question














      I wanna declare new extension method which similar to List.ForEach Method.



      What I wanna archive:



      var dict = new Dictionary<string, string>()

      "K1", "V1" ,
      "K2", "V2" ,
      "K3", "V3" ,
      ;


      dict.ForEach((x, y) =>

      Console.WriteLine($"(Key: x, value: y)");
      );


      How can I do that?







      c#






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 22 '15 at 13:24









      FooFoo

      1




      1






















          4 Answers
          4






          active

          oldest

          votes


















          3














          You can write an extension method easily:



          public static class LinqExtensions

          public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> invoke)

          foreach(var kvp in dictionary)
          invoke(kvp.Key, kvp.Value);




          Using like this:



          dict.ForEach((x, y) => 

          Console.WriteLine($"(Key: x, value: y)");
          );


          Produces



          Key: K1, value: V1
          Key: K2, value: V2
          Key: K3, value: V3





          share|improve this answer






























            3














            Try the following:



            var dict = new Dictionary<string, string>()

            "K1", "V1" ,
            "K2", "V2" ,
            "K3", "V3" ,
            ;

            foreach(KeyValuePair<string, string> myData in dict )

            // Do something with myData.Value or myData.Key



            This is the extension method:



            public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> invokeMe)

            foreach(var keyValue in dictionary)

            invokeMe(keyValue.Key, keyValue.Value);







            share|improve this answer

























            • No need to use lambda expression inside the loop?

              – Foo
              Dec 22 '15 at 13:29











            • what exactly you want to achieve here? i mean any adddelete things @HappyCoding?

              – Neel
              Dec 22 '15 at 13:32











            • Your answer is not matched. I've shown pseudocode above

              – Foo
              Dec 22 '15 at 13:35











            • sorry i missed extension method part. updated now @HappyCoding

              – Neel
              Dec 22 '15 at 13:37


















            1














            1: Extension methods must be declared in a non-nested, non-generic, static class.

            2: The 1st parameter must be annotated with the this keyword.



            public static class DictionaryExtensions

            public static void ForEach<TKey, TValue>(
            this Dictionary<TKey, TValue> dictionary,
            Action<TKey, TValue> action)

            foreach (KeyValuePair<TKey, TValue> pair in dictionary)
            action(pair.Key, pair.Value);





            This method can then be called as if it were a regular instance method:



            dict.ForEach((key, value) =>
            Console.WriteLine($"(Key: key, Value: value)"));





            share|improve this answer






























              1














              var dic = new Dictionary<string, string>();
              dic.Add("hello", "bob");
              dic.Foreach(x =>

              Console.WriteLine(x.Key + x.Value);
              );


              public static void Foreach<T, TY>(this Dictionary<T, TY> collection, Action<T, TY> action)

              foreach (var kvp in collection)

              action.Invoke(kvp.Key, kvp.Value);







              share|improve this answer




















              • 1





                action.Invoke(kvp.Key, kvp.Value); can be shortened to action(kvp.Key, kvp.Value);

                – Zdeslav Vojkovic
                Dec 22 '15 at 13:34










              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34416931%2fdictionarytkey-tvalue-foreach-method%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              You can write an extension method easily:



              public static class LinqExtensions

              public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> invoke)

              foreach(var kvp in dictionary)
              invoke(kvp.Key, kvp.Value);




              Using like this:



              dict.ForEach((x, y) => 

              Console.WriteLine($"(Key: x, value: y)");
              );


              Produces



              Key: K1, value: V1
              Key: K2, value: V2
              Key: K3, value: V3





              share|improve this answer



























                3














                You can write an extension method easily:



                public static class LinqExtensions

                public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> invoke)

                foreach(var kvp in dictionary)
                invoke(kvp.Key, kvp.Value);




                Using like this:



                dict.ForEach((x, y) => 

                Console.WriteLine($"(Key: x, value: y)");
                );


                Produces



                Key: K1, value: V1
                Key: K2, value: V2
                Key: K3, value: V3





                share|improve this answer

























                  3












                  3








                  3







                  You can write an extension method easily:



                  public static class LinqExtensions

                  public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> invoke)

                  foreach(var kvp in dictionary)
                  invoke(kvp.Key, kvp.Value);




                  Using like this:



                  dict.ForEach((x, y) => 

                  Console.WriteLine($"(Key: x, value: y)");
                  );


                  Produces



                  Key: K1, value: V1
                  Key: K2, value: V2
                  Key: K3, value: V3





                  share|improve this answer













                  You can write an extension method easily:



                  public static class LinqExtensions

                  public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> invoke)

                  foreach(var kvp in dictionary)
                  invoke(kvp.Key, kvp.Value);




                  Using like this:



                  dict.ForEach((x, y) => 

                  Console.WriteLine($"(Key: x, value: y)");
                  );


                  Produces



                  Key: K1, value: V1
                  Key: K2, value: V2
                  Key: K3, value: V3






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 22 '15 at 13:34









                  RobRob

                  23.8k115575




                  23.8k115575























                      3














                      Try the following:



                      var dict = new Dictionary<string, string>()

                      "K1", "V1" ,
                      "K2", "V2" ,
                      "K3", "V3" ,
                      ;

                      foreach(KeyValuePair<string, string> myData in dict )

                      // Do something with myData.Value or myData.Key



                      This is the extension method:



                      public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> invokeMe)

                      foreach(var keyValue in dictionary)

                      invokeMe(keyValue.Key, keyValue.Value);







                      share|improve this answer

























                      • No need to use lambda expression inside the loop?

                        – Foo
                        Dec 22 '15 at 13:29











                      • what exactly you want to achieve here? i mean any adddelete things @HappyCoding?

                        – Neel
                        Dec 22 '15 at 13:32











                      • Your answer is not matched. I've shown pseudocode above

                        – Foo
                        Dec 22 '15 at 13:35











                      • sorry i missed extension method part. updated now @HappyCoding

                        – Neel
                        Dec 22 '15 at 13:37















                      3














                      Try the following:



                      var dict = new Dictionary<string, string>()

                      "K1", "V1" ,
                      "K2", "V2" ,
                      "K3", "V3" ,
                      ;

                      foreach(KeyValuePair<string, string> myData in dict )

                      // Do something with myData.Value or myData.Key



                      This is the extension method:



                      public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> invokeMe)

                      foreach(var keyValue in dictionary)

                      invokeMe(keyValue.Key, keyValue.Value);







                      share|improve this answer

























                      • No need to use lambda expression inside the loop?

                        – Foo
                        Dec 22 '15 at 13:29











                      • what exactly you want to achieve here? i mean any adddelete things @HappyCoding?

                        – Neel
                        Dec 22 '15 at 13:32











                      • Your answer is not matched. I've shown pseudocode above

                        – Foo
                        Dec 22 '15 at 13:35











                      • sorry i missed extension method part. updated now @HappyCoding

                        – Neel
                        Dec 22 '15 at 13:37













                      3












                      3








                      3







                      Try the following:



                      var dict = new Dictionary<string, string>()

                      "K1", "V1" ,
                      "K2", "V2" ,
                      "K3", "V3" ,
                      ;

                      foreach(KeyValuePair<string, string> myData in dict )

                      // Do something with myData.Value or myData.Key



                      This is the extension method:



                      public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> invokeMe)

                      foreach(var keyValue in dictionary)

                      invokeMe(keyValue.Key, keyValue.Value);







                      share|improve this answer















                      Try the following:



                      var dict = new Dictionary<string, string>()

                      "K1", "V1" ,
                      "K2", "V2" ,
                      "K3", "V3" ,
                      ;

                      foreach(KeyValuePair<string, string> myData in dict )

                      // Do something with myData.Value or myData.Key



                      This is the extension method:



                      public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> invokeMe)

                      foreach(var keyValue in dictionary)

                      invokeMe(keyValue.Key, keyValue.Value);








                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 13 '18 at 0:35









                      Lauren Van Sloun

                      94911018




                      94911018










                      answered Dec 22 '15 at 13:26









                      NeelNeel

                      9,93512949




                      9,93512949












                      • No need to use lambda expression inside the loop?

                        – Foo
                        Dec 22 '15 at 13:29











                      • what exactly you want to achieve here? i mean any adddelete things @HappyCoding?

                        – Neel
                        Dec 22 '15 at 13:32











                      • Your answer is not matched. I've shown pseudocode above

                        – Foo
                        Dec 22 '15 at 13:35











                      • sorry i missed extension method part. updated now @HappyCoding

                        – Neel
                        Dec 22 '15 at 13:37

















                      • No need to use lambda expression inside the loop?

                        – Foo
                        Dec 22 '15 at 13:29











                      • what exactly you want to achieve here? i mean any adddelete things @HappyCoding?

                        – Neel
                        Dec 22 '15 at 13:32











                      • Your answer is not matched. I've shown pseudocode above

                        – Foo
                        Dec 22 '15 at 13:35











                      • sorry i missed extension method part. updated now @HappyCoding

                        – Neel
                        Dec 22 '15 at 13:37
















                      No need to use lambda expression inside the loop?

                      – Foo
                      Dec 22 '15 at 13:29





                      No need to use lambda expression inside the loop?

                      – Foo
                      Dec 22 '15 at 13:29













                      what exactly you want to achieve here? i mean any adddelete things @HappyCoding?

                      – Neel
                      Dec 22 '15 at 13:32





                      what exactly you want to achieve here? i mean any adddelete things @HappyCoding?

                      – Neel
                      Dec 22 '15 at 13:32













                      Your answer is not matched. I've shown pseudocode above

                      – Foo
                      Dec 22 '15 at 13:35





                      Your answer is not matched. I've shown pseudocode above

                      – Foo
                      Dec 22 '15 at 13:35













                      sorry i missed extension method part. updated now @HappyCoding

                      – Neel
                      Dec 22 '15 at 13:37





                      sorry i missed extension method part. updated now @HappyCoding

                      – Neel
                      Dec 22 '15 at 13:37











                      1














                      1: Extension methods must be declared in a non-nested, non-generic, static class.

                      2: The 1st parameter must be annotated with the this keyword.



                      public static class DictionaryExtensions

                      public static void ForEach<TKey, TValue>(
                      this Dictionary<TKey, TValue> dictionary,
                      Action<TKey, TValue> action)

                      foreach (KeyValuePair<TKey, TValue> pair in dictionary)
                      action(pair.Key, pair.Value);





                      This method can then be called as if it were a regular instance method:



                      dict.ForEach((key, value) =>
                      Console.WriteLine($"(Key: key, Value: value)"));





                      share|improve this answer



























                        1














                        1: Extension methods must be declared in a non-nested, non-generic, static class.

                        2: The 1st parameter must be annotated with the this keyword.



                        public static class DictionaryExtensions

                        public static void ForEach<TKey, TValue>(
                        this Dictionary<TKey, TValue> dictionary,
                        Action<TKey, TValue> action)

                        foreach (KeyValuePair<TKey, TValue> pair in dictionary)
                        action(pair.Key, pair.Value);





                        This method can then be called as if it were a regular instance method:



                        dict.ForEach((key, value) =>
                        Console.WriteLine($"(Key: key, Value: value)"));





                        share|improve this answer

























                          1












                          1








                          1







                          1: Extension methods must be declared in a non-nested, non-generic, static class.

                          2: The 1st parameter must be annotated with the this keyword.



                          public static class DictionaryExtensions

                          public static void ForEach<TKey, TValue>(
                          this Dictionary<TKey, TValue> dictionary,
                          Action<TKey, TValue> action)

                          foreach (KeyValuePair<TKey, TValue> pair in dictionary)
                          action(pair.Key, pair.Value);





                          This method can then be called as if it were a regular instance method:



                          dict.ForEach((key, value) =>
                          Console.WriteLine($"(Key: key, Value: value)"));





                          share|improve this answer













                          1: Extension methods must be declared in a non-nested, non-generic, static class.

                          2: The 1st parameter must be annotated with the this keyword.



                          public static class DictionaryExtensions

                          public static void ForEach<TKey, TValue>(
                          this Dictionary<TKey, TValue> dictionary,
                          Action<TKey, TValue> action)

                          foreach (KeyValuePair<TKey, TValue> pair in dictionary)
                          action(pair.Key, pair.Value);





                          This method can then be called as if it were a regular instance method:



                          dict.ForEach((key, value) =>
                          Console.WriteLine($"(Key: key, Value: value)"));






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 22 '15 at 13:35









                          Dennis_EDennis_E

                          7,4581421




                          7,4581421





















                              1














                              var dic = new Dictionary<string, string>();
                              dic.Add("hello", "bob");
                              dic.Foreach(x =>

                              Console.WriteLine(x.Key + x.Value);
                              );


                              public static void Foreach<T, TY>(this Dictionary<T, TY> collection, Action<T, TY> action)

                              foreach (var kvp in collection)

                              action.Invoke(kvp.Key, kvp.Value);







                              share|improve this answer




















                              • 1





                                action.Invoke(kvp.Key, kvp.Value); can be shortened to action(kvp.Key, kvp.Value);

                                – Zdeslav Vojkovic
                                Dec 22 '15 at 13:34















                              1














                              var dic = new Dictionary<string, string>();
                              dic.Add("hello", "bob");
                              dic.Foreach(x =>

                              Console.WriteLine(x.Key + x.Value);
                              );


                              public static void Foreach<T, TY>(this Dictionary<T, TY> collection, Action<T, TY> action)

                              foreach (var kvp in collection)

                              action.Invoke(kvp.Key, kvp.Value);







                              share|improve this answer




















                              • 1





                                action.Invoke(kvp.Key, kvp.Value); can be shortened to action(kvp.Key, kvp.Value);

                                – Zdeslav Vojkovic
                                Dec 22 '15 at 13:34













                              1












                              1








                              1







                              var dic = new Dictionary<string, string>();
                              dic.Add("hello", "bob");
                              dic.Foreach(x =>

                              Console.WriteLine(x.Key + x.Value);
                              );


                              public static void Foreach<T, TY>(this Dictionary<T, TY> collection, Action<T, TY> action)

                              foreach (var kvp in collection)

                              action.Invoke(kvp.Key, kvp.Value);







                              share|improve this answer















                              var dic = new Dictionary<string, string>();
                              dic.Add("hello", "bob");
                              dic.Foreach(x =>

                              Console.WriteLine(x.Key + x.Value);
                              );


                              public static void Foreach<T, TY>(this Dictionary<T, TY> collection, Action<T, TY> action)

                              foreach (var kvp in collection)

                              action.Invoke(kvp.Key, kvp.Value);








                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 13 '18 at 0:41









                              Lauren Van Sloun

                              94911018




                              94911018










                              answered Dec 22 '15 at 13:31









                              MacMac

                              184




                              184







                              • 1





                                action.Invoke(kvp.Key, kvp.Value); can be shortened to action(kvp.Key, kvp.Value);

                                – Zdeslav Vojkovic
                                Dec 22 '15 at 13:34












                              • 1





                                action.Invoke(kvp.Key, kvp.Value); can be shortened to action(kvp.Key, kvp.Value);

                                – Zdeslav Vojkovic
                                Dec 22 '15 at 13:34







                              1




                              1





                              action.Invoke(kvp.Key, kvp.Value); can be shortened to action(kvp.Key, kvp.Value);

                              – Zdeslav Vojkovic
                              Dec 22 '15 at 13:34





                              action.Invoke(kvp.Key, kvp.Value); can be shortened to action(kvp.Key, kvp.Value);

                              – Zdeslav Vojkovic
                              Dec 22 '15 at 13:34

















                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34416931%2fdictionarytkey-tvalue-foreach-method%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Use pre created SQLite database for Android project in kotlin

                              Darth Vader #20

                              Ondo