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 );

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was 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 Microsoft MVP.
This entry was posted in Patterns & Skills. Bookmark the permalink.

7 Responses to Lambda–Not as weird as it sounds

Comments are closed.