Lambda Expressions. Other than LINQ…

… what are they good for?

If you haven’t caught on yet, I’m inching my way toward LINQ. By the time I get to it, it will probably be about a 5 – 10 minute presentation just like the other presentations I’ve done.  My goal is to give you enough to get your job done without all the whys and wherefores you’ll get from just about all the other presentations.  That is, I’m not going to give you an hour presentation on LINQ when 5 minutes will do the trick.  If you want that much detail, there are several other presentations available on the web that will fill the gap.

So, the next step in the LINQ puzzle is Lambda Expressions.  To understand lambda expressions, you first need to understand their little brother, anonymous methods.

All an anonymous method is, is a really simple way of creating a delegate.  If all I really need is a function that’s going to loop through an array, it’s a whole lot easier to setup the array and use it IN the anonymous method than it is to pass the array to the delegate before I can start using it.  This is probably easier to illustrate in code, so here’s an example:

        int[] iArray = new int[3];
        iArray[0] = 2;
        iArray[1] = 1;
        iArray[2] = 5;
        var ts = new System.Threading.ThreadStart(delegate()
            { foreach(int i in iArray)

You’ll notice that I’ve created a delegate that is using an array that I created prior to creating the delegate.  If I had done this using a standard delegate, I would have had to create a delegate that took the array as a parameter.  Something, I’ve actually had to do in earlier versions of .NET.  Let me tell you it isn’t pretty.  In fact, for all of you who might want to argue that using anonymous methods makes the code unreadable.  This right here should prove that it isn’t.  In fact, it’s so ugly, I’m not even going to show you an example.

VB.NET really has no equivalent to an anonymous method, even now that Lambda Expressions are available in the language.

So, what about those lambda expressions?

In csharp, we are replacing delegate(parametersHere) with (parametersHere)=>

It looks a lot like LISP if you ask me.

So, the code above, becomes:

        int[] iArray = new int[3];
        iArray[0] = 2;
        iArray[1] = 1;
        iArray[2] = 5;
        var ts = new System.Threading.ThreadStart(()=>
            { foreach(int i in iArray)

VB.NET has lambda expressions too.  But, in my opinion, they aren’t as rich.  The VB.NET documentation states:

A lambda expression is a function without a name that evaluates a single expression and returns its value.

Which means we can’t have a multi line statement as part of our lambda expression.  So, here’s what we are left with:

        Dim i = Foo((Function(n As Integer) n + 23)(6))

Where Foo() is a function that takes an integer as a parameter and returns an integer.

What we’ve done here is created a delegate:

                   Function(n As Integer) n + 23

Which is an anonymous function that takes an integer as a parameter and adds 23 to it and returns the integer.  This delegate can then be assigned to a variable which will act like a function.

        Dim x = Function(n As Integer) n + 23
        i = x(6)

We can skip the assignment to x by wrapping the expression in parenthesis and then passing the parameter at the end

        (Function(n As Integer) n + 23)(6)
Given the choice, I think I'd rather use Lambda Expressions in csharp than in VB.NET.

Technorati Tags: ,,, Tags: ,,,

Related Post

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail