What is a lambda expression?
I’ve got a few comments about my blog posts not being easy to follow, so I’ve split the next topic in two posts. In the next post I'll show you how I did (sort-of) continuations using lambda expressions, to be able to visualize different states in an algorithm. If you know what lambda expressions are you can ignore this post.
Code is data too
Lambda expressions are the fundamental concept of lambda calculus. If you ever touched a functional programming language you already know how to use them. I won't get into the theory behind them, only enough for you to use them in C#. Simply put: in C#, a lambda expression is a dead-easy and elegant way to declare an anonymous function. Anonymous functions are as the name implies functions without names, which in practice means that they are not declared as either class or instance functions, but rather as data. JavaScript has a very clear way to define anonymous functions, so I’ll use that as a first example:
var myFunction = function (x) { return x*x*x; };
As you can see, anonymous functions are stored in variables. You call the function by calling the variable, for example myFunction(4); Anonymous functions are very useful, they can be used to execute code when a file transfer is finished or when a button is clicked. In C#, prior to version 3.0, the way to define anonymous functions was like this:
// inside a class or namespace, declare a delegate prototype: delegate int MyFunctionDelegate(int x); // inside a code block: MyFunctionDelegate myFunction = delegate (int x) { return x*x*x ; };
The first line defines the function signature or prototype, because in C#, anonymous methods are strictly typed. The second line is more similar to the JavaScript code, but two important things have changed; we must use a type for the variable, this is the type we defined on the first line. The second is that “function” has changed to “delegate”. A little less clear IMHO, but it gets better. In C# 3.0, lambda expressions were introduced, and along with them, a bunch of predefined prototypes. I will not go in to those here, except for the one I will use in the next post, Action, which is a prototype for functions that does not return anything and takes no arguments (OK, the other important one is Func):
public delegate void Action();
A lambda expression is a short way of defining an anonymous method. The only thing you need to write is the argument names in parentheses followed by “=>” and followed by an expression. The types are implicitly defined by the prototype, so the declaration is very compact. Here is an example:
using System; public class LambdaExpressionExample { static Action myFunction; public static void Main() { // store a method myFunction = () => Console.WriteLine("Hello world"); // outputs "Hello, world" myFunction(); // store another method myFunction = () => Console.WriteLine("Good bye, cruel world"); // outputs "Good bye, cruel world" myFunction(); Console.ReadLine(); } }
Comments
No comments so far. You leave the first:
© 2008 Markus Johnsson