Lambda–Not as weird as it sounds

Windows Phone From Scratch #36

Yesterday I wrote a few posts on LINQ but carefully avoided using Lambda expressions. Now it is time to look at this feature that sounds fierce but is really fairly straight-forward.

Lambda expressions are just a short-hand notation for declaring delegates.

To see this at work, let’s begin by creating a very simple (if absurd) program that declares a delegate to a method that takes two integers and returns a string, and then uses that delegate to call the method.

 

public MainPage( )
{
   InitializeComponent( );
   Func<int, int, string> AddDelegate = Add;
   string result = AddDelegate( 5, 7 );
   TheListBox.Items.Add( result );
}

private string Add( int a, int b )
{
   return ( a + b ).ToString( );
}

Taking this apart, on line 4 we declare AddDelegate to be a delegate to a method that takes two integers and returns a string, and we assign that delegate to the Add method declared at the bottom of the listing.  We then invoke the method through that delegate on line 5, and on line 6 we add the result to the listbox we created in MainPage.xaml.

Lambda syntax allows us to write the same thing much more tersely. 

Func<int, int, string> AddLambda = 
   ( a, b ) => ( ( a + b ).ToString( ) );
result = AddLambda( 3, 8 );
TheListBox.Items.Add( result );

 

The first line (broken over two lines to fit this blog) declares AddLambda to be a Lambda expression for a method that takes two integers and returns a string, and then replaces the body of the method it points to with what is written to the right of the equal sign.  Immediately to the right of the equal sign, in parentheses are the parameters.  This is followed by the lambda symbol (=>) usually pronounced “goes to” and that is followed by the work of the anonymous method. 

Lay them out side by side and the left hand side is identical

Func<int, int, string> AddDelegate = 
Func<int, int, string> AddLambda =

To the right of the equal sign the method takes its parameters and the Lambda takes its parameters

( int a, int b )
( a, b )

The lambda, however, uses type inference to infer the type of the parameters. 

The body of the method and the body of the lambda expression are nearly identical

private string Add( int a, int b )
{
   return ( a + b ).ToString( );
}

 => ( ( a + b ).ToString( ) );

 

Once you have the mapping in your head, it all becomes a trivial exercise, and you quickly become very fond of lambda expressions because they are easier to type, shorter, easier to maintain, etc.

Note that if you do not have any parameters for the lambda, you indicate that with empty parentheses

() => ( // some work );
Share

About Jesse Liberty

Jesse Liberty is an independent consultant and programmer with three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP, Microsoft MVP and Telerik MVP.
This entry was posted in Patterns & Skills. Bookmark the permalink.

8 Responses to Lambda–Not as weird as it sounds

  1. Jordan Leemow says:

    Younger lambs are smaller and more tender. Mutton is meat from a sheep over two years old, and has less tender flesh. In general, the darker the colour, the older the animal. Baby lamb meat will be pale pink, while regular lamb is pinkish-red.,-:..

    http://www.healthmedicinelab.comMy own, personal web blog

  2. mohdrusmanarief says:

    to anonymous:
    8 lines (using lambda) is obviously shorter than the original one (12 lines).

  3. Marc says:

    We use lambdas a lot within framework- and app-development too. They really reduce the clutter around the main purpose of the code.

  4. Duncan Watts says:

    One of the main things that you get with lamda expressions that isn’t mentioned here is that the execution of the lambda expression is within the scope of the method which defines it. This means you can reference variables outside of the lamda expression within it, which can simplify some code.

  5. Anonymous says:

    I dont get it…this sample looks longer to do lambdas?

  6. Pingback: Tweets that mention Lambda

Leave a Reply

Your email address will not be published.