views:

768

answers:

2

When should anonymous methods be used when defining a delegate and when should formally defined methods be used when defining a delegate ?

+4  A: 

I use anonymous methods when the function that should be executed, should only be executed by that delegate (in other words: when I do not need that function in any other place), and, when the function/method that has to be executed is relatively short (5 lines max.).

But, there are no strict rules defined when to use what.
IMHO, I find that anonymous methods do not contribute to readability in most of the situations, so I mostly do not use them.

Frederik Gheysels
Ah, that makes sense re: relatively short length. Thanks for the opinion on readability, as well, as I was finding the anonymous syntax a little more complex and I wondered if that was just me!
Scott Davies
+10  A: 

If you need to use the same logic in more than one place, it makes sense to use a separate method.

If you only need to use the logic once and it's fairly short, it makes sense to use an anonymous function. If the delegate needs access to local variables in the method which is creating it, anonymous functions act as closures which can also be very handy.

Additionally, an anonymous function can be useful even if it's reasonably long if it's used for something like parallelization with Parallel Extensions - part of the point of that is that you can take existing serial code and parallelise it "in place" to a large extent.

You might also want to consider testability - if your delegate's code is sufficiently complicated that it warrants its own unit tests, exposing it as a method makes a lot of sense. (Unfortunately it would have to be either an internal method using InternalsVisibleTo or a public method, where often you'd normally want it to be private, but such is life.)

Jon Skeet
Separation of logic was something I hadn't considered and the ease of access to local variables without passing parameters is interesting. Are Parallel Extensions a C# 4.0 feature ?
Scott Davies
I'd like to add that the two aren't mutually exclusive. It's often handy to have an anonymous function that mostly calls a regular function, to get the advantages of reusability and closures at the same time.
Steven Sudit
Yes, Parallel Extensions ship with 4.0.
Steven Sudit
Thank you, Steven. Have you been working with the VS 2010 Beta ? Would you recommend that I begin evaluating it now ? As far as I know, it's scheduled for RTM 4th quarter of this year.
Scott Davies
Note that Parallel Extensions is a .NET 4.0 feature, not a C# 4.0 feature. I always like to separate language versions from platform versions. I haven't seen any RTM dates for VS2010 btw, and Q4 sounds fairly optimistic to me. I suspect it will genuinely be in 2010. Then again, that's partially just being hopeful in terms of my book's 2nd edition not being out *much* later than the product :)
Jon Skeet
I find the ability to capture local variables in a closure to be very handy. As to VS 2010, so far it is quite good. I have been using it to program C# 3.0 and C++ for about a month now. There are a few stability issues with it and for many people typing may feel "slow" and sluggish (noted on Rico Mariani's Blog http://blogs.msdn.com/ricom/). If I were to use it for any serious development I would likely wait until Beta 2 when a few more of the issues are worked out.
smaclell
@smalcell - Thank you for your input, especially regarding stability. I will proceed with it in a VM, which will degrade performance, but isolate my main development environment.@Jon Skeet - Too cool! I bought the first edition of your book and it's next in the queue for my reading. Manning books are great! Does the second edition feature any updates on C# 3.0 ? I will MEAP it, if so.
Scott Davies
@Scott: Yes, there'll be some updates to the existing text (although I haven't started on them yet) - most of the changes will be new chapters for C# 4 though.
Jon Skeet
@Scott: I have little to add to Jon's comprehensive and correct answers except a general endorsement of the architecture behind Parallel Extensions. It's definitely a move in the right direction, and can make a huge difference in the performance of your application.
Steven Sudit