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

  • Anonymous TypesAnonymous Types So, let's get to work looking at anonymous types in .NET. As you know, last week we looked at object initialization.  Object initialization allows us to instantiate an object and assign pr...
  • Object Initialization in CSharp 3.0 and VB.NET 9Object Initialization in CSharp 3.0 and VB.NET 9 Yesterday we looked at the new var keyword in CSharp.  This makes CSharp variable declaration similar to VB.  After all, they've had the DIM keyword for years which essentially does the same thing....
  • Extension Methods in VB.NETExtension Methods in VB.NET Last week we took a look at Extension Methods in CSharp.  Today we will continue looking at Extension methods using VB.NET syntax.  It's obvious by comparing the syntax in VB.NET to the syntax we u...
  • Intellisense Everywhere in VB 9 (and a small bug)Intellisense Everywhere in VB 9 (and a small bug) One of the features the CSharp guys have had for quite a while is Intellisense everywhere.  Want to create a private variable?  Just start typing private and hit the tab key as soon as the word is ...
  • Computed Columns Using LINQ to SQL ClassesComputed Columns Using LINQ to SQL Classes Last week we looked at the extension points Microsoft has wired into the LINQ to SQL classes and how they can be used to achieve some of the capabilities of the Business Logic Layer (BLL) in a mult...