.Net evolution towards Lambda expressions

When I was at uSwitch we used to have a weekly brown bag session whereby a volunteer would give their time to do a presentation on a new or useful piece of technology that they could share with the team. I remember one such session which I enjoyed a lot by Mike Wagg about how Lambda expressions came about in .Net as it helped me understand exactly what they were for.

When I joined my new company, I decided I wanted to implement similar sessions to help the guys to do the same here i.e. learn new technologies and share best practices within the team. So I’ve started running (almost) weekly Pret Sessions (because we have a Pret a Manger just nearby & it’s the easiest way to quickly pick up some lunch & still do a session). We’ve had a few sessions on TDD & Web development tools but last week I was asked to run a session about Lambdas so I did my own interpretation of Mike’s topic and thought I’d blog about it in case anybody else found it useful.

So here goes.

The first thing I did was set up a Unit Test project (of course – anyone who knows me knows I’m a TDD/BDD bore!) and then I created a simple class called Delegator_Tests with a single test:

I decided to test a very simple function for the demo so I created a class TDD style called Delegator with a method called AddTest which just took 2 parameters and tested the method added them together (note that my should_be_equal_to notation is just a BDD style extension method inspired by my time at uSwitch – it’s the same as Assert.That(result, Is.EqualTo(3)).

Using the Red/Green/Refactor mantra I ran my unit tests – Fail – of course it did, I didn’t implemented any functionality. I amended the Delegator class:

and re-ran my tests – Pass – Hooray.

I started to refactor by amending the Delegator code to use the delegate notation introduced in C# 1.x. A few of the guys in the group knew about this technique but hadn’t implemented it very often so it prompted some good discussion. In the code:

  1. I created a new internal delegate called Calculate with the same signature we needed for the method
  2. I moved the existing addition to an internal method called AddNumbers
  3. I created an internal variable delegate called add, passed the internal method in the constructor and then called Invoke to return the delegated value.

And re-ran my unit tests – still Pass – Hooray again!

The general feeling from the example was that it seemed like overkill – which or course it is for such a simple example – but I stressed that I was trying to demo how delegates work and we discussed other uses for this type of functionality.

Once everybody was happy I refactored the code again to reflect the changes which occurred in C# 2.0:

  1. I replaced the concrete instantiation of the Calculate
  2. delegate with an anonymous delegate with the same signature

  3. I removed the AddNumbers private method and brought that functionality directly into the anonymous method
  4. I then just returned the add delegate with the original parameters

And of course re-ran my unit tests yet again – still Pass – lots more Hoorays!

This went down ok with the group, everyone seemed to be grasping how we can use delegates in this way. So after a few more minutes discussion we moved onto how this notation evolved to a Lambda expression in C# 3.0:

One final unit test re-run and as we’ve come to expect – still Pass – one final Hooray!

This is obviously even less of a jump from anonymous delegates and so everyone was happier with this. It prompted some final discussion on the lambda notation and then moved towards using generic Action<T> classes to help delegate responsbility even further by allowing lambdas to be passed around through parameters.

So that’s a very quick overview of how the .Net framework evolved towards Lambda expressions. It was an interesting session for me to run and hopefully my guys learned at least the basics – I’m looking forward to seeing lot’s more Lambdas in our code base!

Until next time…

Advertisement

Tags: , , , , ,

3 Responses to “.Net evolution towards Lambda expressions”

  1. Steve Lloyd Says:

    Nice example. It’s good to see a progression in the readability of the code, even though it does the same thing you can tell pretty much immediatly what this method is doing.

  2. Mike Wagg Says:

    Nice Work. I have started writing up my thoughts on lambdas and their application with linq to objects. It should be done soon if I can only get some spare time :-)

  3. Chris Bishop Says:

    I haven’t used Lambda expressions much but I am working with code that uses them profusely with Action objects.

    This has been the best article that I have come across to show how they work or better yet how the replace existing verbose code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.