Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Anonymous Methods and Types

Anonymous Methods and Types

Scheduled Pinned Locked Moved The Lounge
csharplinqquestiondiscussion
39 Posts 23 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michael Bookatz
    wrote on last edited by
    #1

    Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?

    Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!

    L R D N A 17 Replies Last reply
    0
    • M Michael Bookatz

      Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?

      Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      No. You just need to learn to love them. Update: The best thing about them are that they are closures. Closures are the poor man's objects*. Objects are the poor man's closures. * As in OOP, eg classes

      xacc.ide
      IronScheme - 1.0 RC 1 - out now!
      ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition

      modified on Wednesday, March 10, 2010 1:11 PM

      1 Reply Last reply
      0
      • M Michael Bookatz

        Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?

        Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!

        R Offline
        R Offline
        Rama Krishna Vavilala
        wrote on last edited by
        #3

        It really depends on the situation. I have used them in thread callback methods, where it saves me for creating a one lined method. My standard is that if Anonymous methods over 3 lines they better be made into a regular method. In case of lambda expressions, I do not go beyond a single statement.

        1 Reply Last reply
        0
        • M Michael Bookatz

          Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?

          Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!

          D Offline
          D Offline
          Dave Parker
          wrote on last edited by
          #4

          What does my head in are the clowns who insist on replacing every single foreach loop with a ForEach lambda and calling it refactoring. I do kinda prefer typing => to having the word 'delegate' and type names repeated everywhere though.

          1 Reply Last reply
          0
          • M Michael Bookatz

            Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?

            Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #5

            Anonymous types are handy when you want to databind properties that are not directly available in the form you want them to be in. Without anonymous types, you'd end up creating an inner private struct or class merely for databinding which is kinda overkill.

            Regards, Nish


            Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
            My latest book : C++/CLI in Action / Amazon.com link

            B 1 Reply Last reply
            0
            • M Michael Bookatz

              Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?

              Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!

              A Offline
              A Offline
              Andy Brummer
              wrote on last edited by
              #6

              There are some designs where they lead to easier to read code. I have some code that renders a tree view I use this with different lists of objects. I take 2 delegates that provide a grouping key and an area for each node. In the past I would have added an interface to each object I wanted to use, or create methods in the object, or a helper object to pass as parameters, now I just use lambdas. The objects never have to know that they will be used this way, the renderer can be used on any object, and the caller of the renderer is the only code responsible for making sure the objects can be rendered.

              I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

              1 Reply Last reply
              0
              • M Michael Bookatz

                Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?

                Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Because other languages have them and the C# team wants C# to be everything? :~

                S C 2 Replies Last reply
                0
                • M Michael Bookatz

                  Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?

                  Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!

                  S Offline
                  S Offline
                  Single Step Debugger
                  wrote on last edited by
                  #8

                  Very useful when you need to overwrap some component event handler add a new one and then return the original delegate. For example you could catch Mouse Up event over some DataGridView component, save a reference to the original event handler and remove it, add a new one with your code, which makes something useful – modify the behavior when user rearrange the columns and then remove your handler and return the original Mouse Down event handler. All this in a few rows short method. Here is some simple example:

                      void bindGrid\_ColumnDisplayIndexChanged(object sender, DataGridViewColumnEventArgs e)
                      {
                          DataGridView thisGrid = sender as DataGridView;
                  
                          MouseEventHandler mouseEventHandlerToRemove = null;
                          MouseEventHandler mouseEventHandler = delegate(object mSender, MouseEventArgs mouseEventArgs)
                          {
                              foreach (DataGridViewColumn clmn in thisGrid.Columns)
                              {
                                  if (clmn is DataGridViewButtonColumn)
                                  {
                                      clmn.DisplayIndex = (thisGrid.Columns\[clmn.Index - 1\].DisplayIndex + 1);
                                  }
                              }
                  
                              thisGrid.MouseUp -= mouseEventHandlerToRemove;
                          };
                          mouseEventHandlerToRemove = mouseEventHandler;
                          thisGrid.MouseUp += mouseEventHandler;
                      }
                  

                  The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                  1 Reply Last reply
                  0
                  • M Michael Bookatz

                    Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?

                    Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!

                    I Offline
                    I Offline
                    Ian Shlasko
                    wrote on last edited by
                    #9

                    Only one place I use var, aside from LINQ... var something = new This.Really.Long.Namespace.Definition.Wastes.Lots_Of.Space.AndMakes.TheCode.Look.Sloppy(); Only when it's all together like this, so there's no question what type the variable is. (Obviously, if I was using this several times in the same code file, I'd shorten it with 'using' instead, but not for a one-off) As for anonymous methods... They're great in LINQ, whether in the LINQ syntax or just the LINQ extensions (ListOfStuff.Select(a=>a.Name).OrderBy(a=>a.SubString(2));)... I might use them on occasion for a quick event handler, but only for trivially-simple ones... It makes for clean-looking code, but it's a bit counter-intuitive, because you have to remember that the anonymous method won't necessarily be executed there and then.

                    Proud to have finally moved to the A-Ark. Which one are you in?
                    Author of Guardians of Xen (Sci-Fi/Fantasy novel)

                    S J 2 Replies Last reply
                    0
                    • M Michael Bookatz

                      Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?

                      Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!

                      R Offline
                      R Offline
                      Rajesh R Subramanian
                      wrote on last edited by
                      #10

                      hopingToCode wrote:

                      Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg

                      Pretty impressive! How long did that take? brain fart. Just noticed you've mentioned the time taken too. :-O I might put myself on a muscle gain program. Best arm size from the past = 18 inches, relaxed. Current size after 3 years of rest for the back injury to heal = 14.2 inches. Target = old size. Going to take ages. :( BTW, I think that an arc trainer could possibly be the best calorie burning machine. I burned 200 calories in 10 minutes this evening. :)

                      “Follow your bliss.” – Joseph Campbell

                      S A E 3 Replies Last reply
                      0
                      • P PIEBALDconsult

                        Because other languages have them and the C# team wants C# to be everything? :~

                        S Offline
                        S Offline
                        Single Step Debugger
                        wrote on last edited by
                        #11

                        This is not entirely correct, the main C #prototypes - Java and Object Pascal has neither anonymous methods nor delegates.

                        The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                        R 1 Reply Last reply
                        0
                        • R Rajesh R Subramanian

                          hopingToCode wrote:

                          Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg

                          Pretty impressive! How long did that take? brain fart. Just noticed you've mentioned the time taken too. :-O I might put myself on a muscle gain program. Best arm size from the past = 18 inches, relaxed. Current size after 3 years of rest for the back injury to heal = 14.2 inches. Target = old size. Going to take ages. :( BTW, I think that an arc trainer could possibly be the best calorie burning machine. I burned 200 calories in 10 minutes this evening. :)

                          “Follow your bliss.” – Joseph Campbell

                          S Offline
                          S Offline
                          Single Step Debugger
                          wrote on last edited by
                          #12

                          Rajesh R Subramanian wrote:

                          Best arm size from the past - 18 inches, relaxed.

                          Holy Cow! :omg:

                          The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                          J R 2 Replies Last reply
                          0
                          • I Ian Shlasko

                            Only one place I use var, aside from LINQ... var something = new This.Really.Long.Namespace.Definition.Wastes.Lots_Of.Space.AndMakes.TheCode.Look.Sloppy(); Only when it's all together like this, so there's no question what type the variable is. (Obviously, if I was using this several times in the same code file, I'd shorten it with 'using' instead, but not for a one-off) As for anonymous methods... They're great in LINQ, whether in the LINQ syntax or just the LINQ extensions (ListOfStuff.Select(a=>a.Name).OrderBy(a=>a.SubString(2));)... I might use them on occasion for a quick event handler, but only for trivially-simple ones... It makes for clean-looking code, but it's a bit counter-intuitive, because you have to remember that the anonymous method won't necessarily be executed there and then.

                            Proud to have finally moved to the A-Ark. Which one are you in?
                            Author of Guardians of Xen (Sci-Fi/Fantasy novel)

                            S Offline
                            S Offline
                            Single Step Debugger
                            wrote on last edited by
                            #13

                            Anonymous type…isn’t this SO gay VB?

                            The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                            I 1 Reply Last reply
                            0
                            • S Single Step Debugger

                              Rajesh R Subramanian wrote:

                              Best arm size from the past - 18 inches, relaxed.

                              Holy Cow! :omg:

                              The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                              J Offline
                              J Offline
                              Joe Simes
                              wrote on last edited by
                              #14

                              18 inches? Sounds more like T-Rex syndrome than a holy cow! :)

                              S 1 Reply Last reply
                              0
                              • N Nish Nishant

                                Anonymous types are handy when you want to databind properties that are not directly available in the form you want them to be in. Without anonymous types, you'd end up creating an inner private struct or class merely for databinding which is kinda overkill.

                                Regards, Nish


                                Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                                My latest book : C++/CLI in Action / Amazon.com link

                                B Offline
                                B Offline
                                Brady Kelly
                                wrote on last edited by
                                #15

                                In other words, a closure.

                                N 1 Reply Last reply
                                0
                                • S Single Step Debugger

                                  Anonymous type…isn’t this SO gay VB?

                                  The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                                  I Offline
                                  I Offline
                                  Ian Shlasko
                                  wrote on last edited by
                                  #16

                                  Nah... Ya know, I originally learned to program in BASIC, and learned windows-based programming in VB3 (MFC was intimidating at that age)... Having long since outgrown that, I know all about the horrible things VB3 lets you do... Anonymous types in C# are NOTHING like the old "Variant" type in VB, even though they share a few letters. Variants are late-bound and utterly evil, but the C# "var" is just a syntax shortcut (The compiler knows the type). Now, the "dynamic" keyword they're adding in the next version... That's going to cause untold misery...

                                  Proud to have finally moved to the A-Ark. Which one are you in?
                                  Author of Guardians of Xen (Sci-Fi/Fantasy novel)

                                  S 1 Reply Last reply
                                  0
                                  • M Michael Bookatz

                                    Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?

                                    Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!

                                    R Offline
                                    R Offline
                                    Rob Philpott
                                    wrote on last edited by
                                    #17

                                    I agree completely. Clarity and simplicity always win the day over slightly more verbose code in my mind. In my first job many years ago I wrote a line of C++ code which took up about 4 lines. I used all the clever things you could do, assignments as expressions, ternary operators and such and marveled at how much I'd managed to fit between semicolons. My boss didn't like it though and taught me the most important lesson I've adhered to ever since - to make everything as clear and simple as possible. If only everyone else would choose simplicity over elegance or cleverness life would be much, much easier.

                                    Regards, Rob Philpott.

                                    R J 2 Replies Last reply
                                    0
                                    • R Rob Philpott

                                      I agree completely. Clarity and simplicity always win the day over slightly more verbose code in my mind. In my first job many years ago I wrote a line of C++ code which took up about 4 lines. I used all the clever things you could do, assignments as expressions, ternary operators and such and marveled at how much I'd managed to fit between semicolons. My boss didn't like it though and taught me the most important lesson I've adhered to ever since - to make everything as clear and simple as possible. If only everyone else would choose simplicity over elegance or cleverness life would be much, much easier.

                                      Regards, Rob Philpott.

                                      R Offline
                                      R Offline
                                      realJSOP
                                      wrote on last edited by
                                      #18

                                      In other words... "Just because you can, doesn't mean you should."

                                      .45 ACP - because shooting twice is just silly
                                      -----
                                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                      -----
                                      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                      1 Reply Last reply
                                      0
                                      • P PIEBALDconsult

                                        Because other languages have them and the C# team wants C# to be everything? :~

                                        C Offline
                                        C Offline
                                        Chris Losinger
                                        wrote on last edited by
                                        #19

                                        bingo. someone needs to tell the C# features team to take a few years off.

                                        image processing toolkits | batch image processing

                                        1 Reply Last reply
                                        0
                                        • S Single Step Debugger

                                          This is not entirely correct, the main C #prototypes - Java and Object Pascal has neither anonymous methods nor delegates.

                                          The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                                          R Offline
                                          R Offline
                                          realJSOP
                                          wrote on last edited by
                                          #20

                                          And you can bet the C# team is having a good snicker over that...

                                          .45 ACP - because shooting twice is just silly
                                          -----
                                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                          -----
                                          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                          S 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups