Anonymous Methods in C# 2.0: What’s Old is New Again!

Paul Kimmel recently wrote an article for informit.com entitled "Anonymous Methods in C# 2.0" where he made the following statement:

Linguistically, I love methods, but as a practical matter anonymous methods may
just be an example
of some inventive person at Microsoft being a bit too clever.

Unfortunately that was the pull-quote used to promote the article on another part of the site. Unfortunate, because I think they are one of the best new features in C#.

I won’t take Paul to task here – I’ll just assume he’s never worked with a similar language feature in any other language, but I was extremely excited when I first saw they were adding anonymous methods to C# 2.0. Anonymous methods are similar to the Smalltalk concept of CodeBlocks which I learned of and fell in love with during my Clipper 5.0 programming days in the early 90’s. Clipper 5.0’s Codeblocks were my favorite language feature because they provided so much power with so much simplicity.  Even that crazy new language Ruby has (Code)Blocks!

I remember the reason they added CodeBlocks to Clipper was to support "User Defined Commands (UDCs)."  UDCs allowed the developer to use the old dBase-style commands and have them translated via a preproccessor into function calls with parameters (I always love it when language designers actually use a language! When they do, they add really useful features!)

So, looking at the dBase REPLACE command, which is roughly equivalent to a SQL UPDATE, the UDC would convert this:

REPLACE Name WITH "Mike Schinkel" FOR Status="X"

Into something like this (forgive me if it’s not 100% correct, it’s been a looong time since I coded in Clipper):

dbreplace( {|| Name:= "Mike Schinkel" }, {|| Status="X" } )

Try doing that without a language feature like CodeBlocks!

During my Clipper 5.0 days, I found the basic benefits of a (Code)block/Anonymous Function are three-fold:

  • They eliminate the need to uniquely name a method when it would probably be called only once
  • They reduce the number of method names you need to track,
  • They allow you to define code physically closer to the using code. 

Maybe I’m wrong, but I’ll bet Paul will soon find anonymous methods to be one of the most useful language features added to C# in v2.0; right up there with Generics!

2 Replies to “Anonymous Methods in C# 2.0: What’s Old is New Again!”

  1. Hi Mike..
    Hahaha.. I was surprised in a good way when i found you got a blog…
    I’ve been holding on to your book for the last 6 months or so because I got an assignment (the subject is named Programming language and paradigms) to write a report on Clipper programming language and its features and stuff…
    Surprisingly enough your book is the only book on Clipper (programming langauge) that i can find in 4 libraries of major universities in Western Australia (well the web catalogs said so). But your book is a great reference… and got some stuff from your blog to… don’t worry I do proper reference for them..

    Thanks so much man….

    Cheers.

  2. Reason 1 violates the Pragmatic (or eXtreme) Programmer that says to not write a function that is only used once and don’t go the reuse route even when it is used twice (even though you might wince).

    Reason 3 falls apart once Reason 1 is discarded

    Reason 2 – I don’t understand the tracking need. If methods are well-named (either initially or through refactoring), the methods are highly cohesive and have low coupling (and the point at the top is observed) ,then these need to track seems to disappear.

Leave a Reply

Your email address will not be published. Required fields are marked *