DCSIMG
Lambda expressions introduction - Maor's Blog

Lambda expressions introduction

NET 2.0 introduced Anonymous Methods. The idea behind anonymous methods it to write methods inline to the code. They are mainly used for small methods that don't require any need for reuse. For example, we have this code:

   1: bool isGreaterThan5(int n)
   2: {
   3:     return n > 5;
   4: }
   5:  
   6: ...
   7:  
   8: int GetGreatersThanFives(List<int> numbers)
   9: {
  10:     return numbers.Find(isGreaterThan5);
  11: }
We can code it with anonymous method:
   1: int GetGreaterThanFives(List<int> numbers)
   2: {
   3:   return numbers.Find(
   4:         delegate(int n) { return n > 5; }
   5:     );
   6: }

c# 3.0 represent cool thing: Lambda expressions.
Lambda Expressions make things easier by allowing you to avoid the anonymous method and that annoying statement block.

Instead

   1: delegate(int n){ return n > 5; }

We can write:

   1: n =>  n > 5

The form of Lambda expressions is:

argument-list => expression

Parameters to Lambda Expressions

The parameters of a lambda expression can be explicitly or implicitly typed. In an explicitly typed parameter list, the type of each expression is explicitly specified. In an implicitly typed parameter list, the types are inferred from the context in which the lambda expression occurs:

   1: (int x) => x + 100         // explicitly typed parameter
   2:  
   3: (x,y) => return x * y;    // implicitly typed parameter

Lambda Expressions with Multiple Parameters

   1: (x, y) =>  return x * y

Lambda Expressions without Parameters

   1: () => Console.WriteLine("Hello world!")      // No parameters

I think that's enough intro to Lambda Expressions.
I will follow up with a part 2 to show more about this cool fteature: Expression Trees, how LINQ uses it to pass code as parameters and more.

Published 07 September 2007 08:22 PM by Maor David-Pur

Comments

# Maor David-Pur said on 09 September, 2007 12:04 AM

Kolbis asked: what goes on behind the lambda expression? How does the compiler translate it and to what?

# Maor David-Pur said on 09 September, 2007 12:06 AM

kolbis, intrersting point. In a nutshell,I wrote an application and fired up ILDASM and select the application.

The anonymous method and the lambda expression essentially compile to the same IL internally. Hence, their executions are similar.

Conclusion: the lambda expression is automatically converted into a corresponding delegate. The fact that it is no longer "strongly-typed", but the compiler is inferring the type.

# Maor David's Blog said on 24 September, 2007 12:44 AM

Language Integrated Query (LINQ) introducing a standard set of operators that can be used to query several

# Maor David - The Blog said on 27 September, 2007 09:27 AM

How many times have you written wrapper methods for objects that in reality you wished were part of the

# Maor David said on 17 May, 2008 04:55 PM

Last week it was exactly one year since I started blogging, so this is my blog first birthday!! Come and read about the blog&#39;s statistics, top posts and more.

# Maor David said on 17 May, 2008 04:56 PM

Last week it was exactly one year since I started blogging, so this is my blog first birthday!! Come and read about the blog&#39;s statistics, top posts and more.

# Maor David said on 17 May, 2008 04:59 PM

Last week it was exactly one year since I started blogging, so this is my blog first birthday!! Come and read about the blog&#39;s statistics, top posts and more.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:

Search

Go

This Blog

News

    RSS

     

    Connect with Me

    Maor's Facebook profile  Follow Maor on Twitter  Maor's profile on Linkedin  Maor in FriendFeed 
           

Syndication