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 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!

    K Offline
    K Offline
    Keith Barrow
    wrote on last edited by
    #28

    I disagree, just because it's possible that anonymous methods are messy, it doesn't follow that they are:

    delegate void Foo(string s);

    static void WriteToConsole(string j)
    {
    System.Console.WriteLine(j);
    }

    public static void RunSnippet()
    {
    Foo bar = WriteToConsole;
    bar("The delegate using the named method is called.");
    }

    is far less terse than:

    delegate void Foo(string s);

    public static void RunSnippet()
    {
    Foo bar = j => System.Console.WriteLine(j);
    bar("The delegate using the named method is called.");
    }

    Once you get used to reading the syntax, sometimes it is just much cleaner to do it this way. If you need to doe something more complicated, then the anonymous method will look messier but, often, the anonymous method should be an actual method on the object type that is being acted on. Ironically, this means the "closure" stuff is cleaner if you get the object encapsulation right.

    Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

    1 Reply Last reply
    0
    • B Brady Kelly

      In other words, a closure.

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

      Brady Kelly wrote:

      In other words, a closure.

      How can an anonymous type be a closure? :~

      Regards, Nish


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

      1 Reply 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.

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #30

        With few exceptions, clear, simple code optimizes much better than convoluted "fancy" code. C# is no different.

        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!

          J Offline
          J Offline
          Jorgen Sigvardsson
          wrote on last edited by
          #31

          On the other hand, if it's such a problem to you, maybe you're in the wrong business?

          -- Kein Mitleid Für Die Mehrheit

          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!

            E Offline
            E Offline
            Ed Poore
            wrote on last edited by
            #32

            Maybe they're just a bit shy?

            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

              E Offline
              E Offline
              Ed Poore
              wrote on last edited by
              #33

              How about doing some proper exercise :-)[^] Seriously though, do a summer's worth (i.e. a few training expeditions and then the main thing) and you'll see how quickly you become fit lugging a 50lbs rucksack around the mountains. You're expected to (as a 16 yro kid) eat a minimum of 3,500-4,500 calories per day just to keep going (you'll probably lose a bit of wait even with eating that much, I did).

              R 1 Reply Last reply
              0
              • E Ed Poore

                How about doing some proper exercise :-)[^] Seriously though, do a summer's worth (i.e. a few training expeditions and then the main thing) and you'll see how quickly you become fit lugging a 50lbs rucksack around the mountains. You're expected to (as a 16 yro kid) eat a minimum of 3,500-4,500 calories per day just to keep going (you'll probably lose a bit of wait even with eating that much, I did).

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

                Thanks, I could try that some time. :)

                “Follow your bliss.” – Joseph Campbell

                1 Reply Last reply
                0
                • S Single Step Debugger

                  Hope you will get back in shape shortly.

                  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
                  Rajesh R Subramanian
                  wrote on last edited by
                  #35

                  Thank you. :)

                  “Follow your bliss.” – Joseph Campbell

                  1 Reply 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)

                    J Offline
                    J Offline
                    Johann Gerell
                    wrote on last edited by
                    #36

                    Ian Shlasko wrote:

                    Only one place I use var

                    var has very little to to with anonymous types[^]. In your example, it just declares a reference to the actual anonymous type.

                    Time you enjoy wasting is not wasted time - Bertrand Russel

                    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!

                      C Offline
                      C Offline
                      CKnig
                      wrote on last edited by
                      #37

                      Just look for FP (or functional programming) themes/books/articles/hypes/whatever ... and you will see that those are THE BEST feature we got in the last couple of years - I combination with closure caps and generics you can finally say that FP made it mainstream (and with F#, Clojure, etc. you can see that many people are very happy about this). I'm using anon. methods almost everywhere (who said "if you have a hammer every problem looks like a nail!"?) I hide variables in closures, I use those for "lazy"-support etc. pp Greetings, Carsten

                      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!

                        J Offline
                        J Offline
                        Justin Karpilo
                        wrote on last edited by
                        #38

                        I use them as a shorthand return object from web service calls from script (they get serialized into JSON anyway).

                        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!

                          C Offline
                          C Offline
                          Crawfis
                          wrote on last edited by
                          #39

                          Here is a Professor's take on it: - Generally we want to create re-usable types, perfect their interfaces, implementation of interfaces and ensure they are robust. Obviously anonymous types can not be used here. - However, quite often we have a need for a type that is very specific to the task at hand and will not be reused. Anonymous types can be used here and do allow better readability in that the code (delegate or type definition) is where the use of it is. Of course I tell my class that these were really required for LINQ.

                          Roger A. Crawfis Associate Professor Computer Science & Engineering The Ohio State University

                          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