C# Action delegates with Lamda expressions

19 November, 2008 § Leave a comment

The other day I was writing a program from a top-down approach. I don’t usually write code like this, unless I’m doing some test driven development. TDD is interesting because it puts the design over the implementation. By top-down I mean that I first wrote the Main method, then worked on fleshing out all the classes that I had envisioned from there. One of my main goals was for very readable code without comments.

My main method looked like this:

public static void Main()
{
  var carLot = new CarLot( "cars.xml" );
  carLot.Each( car =>
    car.StartEngine();
    car.UnlockDriverSideDoor();
  );
}

I’ve used Lamda expressions before when working with Linq and other C# code, but never written my own function before that implements it. I now moved forward with implementing the different classes and methods.

Looking up how to write the program construct to handle that action delegate could have taken me some time, since I didn’t know they were called action delegates. Luckily, Resharper came to the rescue. I gave focus to the method call, hit Alt+Enter, and had Resharper implement the method. This is what it generated:

public void Each( Action<Car> action )
{
  throw new NotImplementedException();
}

I then modified what was generated to come up with this:

public void Each( Action<Car> action )
{
  Car car;
  while( ( car = RetrieveNextCar() ) != null )
  {
    action( car );
  }
}

By writing the Each method, I can now pass a delegate function to be performed on each car in the car lot. My main method reads pretty nicely now, but not much else was added. This same result could have been achieved by just writing the following instead. It doesn’t look as nice though, in my opinion.

public static void Main()
{
  var carLot = new CarLot( "cars.xml" );
  carLot.Each( delegate( Car car ) {
    car.StartEngine();
    car.UnlockDriverSideDoor();
  });
}

There are also anonymous action delegates that look like this:

public static void Main()
{
  var carLot = new CarLot( "cars.xml" );
  int i = 0;
  carLot.Each( () =>
    i++;
  );
}

Note: The anonymous action delegate can’t take any parameters and can’t return anything either.

So now I conclude this post with an open question. The example I have used in this post is somewhat trivial. There are already constructs like ForEach that can take a predicate function. Do you have any ideas for a different use of this ability outside of just looping over each item in a collection and performing a method on them?

Tagged: , , ,

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 )

Google+ photo

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

Connecting to %s

What’s this?

You are currently reading C# Action delegates with Lamda expressions at JAWS.

meta