Click here to Skip to main content
16,020,628 members
Articles / Programming Languages / C#

Lambda Expressions and Expression Trees: An Introduction

Rate me:
Please Sign up or sign in to vote.
4.95/5 (183 votes)
14 Mar 2007CPOL8 min read 290.1K   1.2K   300   34
Introduces C# 3’s lambda expressions and expression trees in an easy-to-understand way, and describes their benefits and uses. Also touches on anonymous delegates.
[The sample code requires the May 2006 LINQ Preview to be installed in order to work.]

Introduction

This article will introduce you to lambda expressions and expression trees – two new related features coming up in the newest version of C# and the .NET runtime. You will learn how to create them, and how to use them to enhance and simplify your C# code. Knowledge of the concepts behind delegates in the .NET framework is assumed.

Let's start by brushing up on anonymous methods, since the concepts behind them will help in understanding lambda expressions.

Anonymous Methods

.NET 2.0 introduced a new construct: anonymous methods. Instead of declaring a named method in your class and then referencing the method by name when creating a delegate:

C#
bool MatchNumbersBelow10(int n)
{
    return n<10;
}

...

int GetNumber(List<int> numbers)
{
   //gets the first number smaller than 10 in the list
    return numbers.Find(MatchNumbersBelow10);
}

...you can write the method directly where it is used:

C#
int GetNumber(List<int> numbers)
{
   //gets the first number smaller than 10 in the list
  return numbers.Find(
        delegate(int n) 
        { 
            return n<10; 
        }
    );
}

As you can see, in the above sample we are passing in a special kind of nameless inline method as a delegate directly to the numbers.Find() method. The advantages of this "anonymous method" syntax are:

<ul">

  • You don't have to clutter up your class with private methods that are only used once in order to pass some custom code to a method.
  • The code can be put in the place it's used in, rather than somewhere else in the class.
  • The method doesn't have to be named.
  • The return type is inferred from the signature of the delegate type that the anonymous method is being cast to.
  • You can reference local variables in the "outer" method from within the anonymous method.

    Anonymous Method Rules

    The rules for defining an anonymous method are simple:

    • Don't declare a return type - it is inferred from the delegate signature.
    • The keyword delegate is used instead of a method name, since anonymous methods are only ever accessed via a delegate.
    • Declare the method's arguments to match the signature of the delegate, just like you would when declaring a normal method to pass as a delegate.
    • Don't declare variables whose names conflict with variables in the outer method in which the anonymous method is declared.

    Lambda Expressions

    C# 3.0 and the .NET 3.0 Runtime introduce a more powerful construct that builds on the anonymous method concept. It allows you to pass an inline expression as a delegate, with minimal syntax. Instead of the anonymous method we declared above:

    C#
    delegate (int n)
    {
        return n<10;
    }

    ...we can do:

    C#
    n =>  n<10 

    It looks shorter and more concise, doesn't it? But how does it work? The basic form for a lambda expression is:

    argument-list => expression

    In the example above, we have an argument named n, implicitly typed as int, then the lambda operator (=>), then an expression which checks to see whether n is smaller than 10. We can use this lambda expression as input for the Find() method:

    C#
    //gets the first number smaller than 10 in the list
    int result=numbers.Find( n=> n<10);

    To understand better how the lambda expression syntax differs from the anonymous method syntax, let's turn our example anonymous method:

    C#
    delegate(int n)
    {
        return n<10;
    } 

    ...into its lambda-expression equivalent:

    C#
    n=> n<10

    We don't need the delegate keyword, so take it out.

    C#
    (int n)
    {
        return n<10;
    }

    Let's replace the braces with a => lambda operator to make it an inline lambda expression.

    C#
    (int n) => return n<10; 

    The return keyword isn't needed (or even legal) because an expression is always a single line of code that returns a value. Also, remove the semicolon, because n<10 is now an expression, not a full statement.

    C#
    (int n)=> n<10 

    Now, that's already a usable lambda expression - but we can simplify it just a bit more. The type of the argument can be inferred as well by the compiler, so we can remove the type declaration for the argument.

    C#
    (n)=> n<10

    We can also take out the parenthesis now, because we don't give the types of the arguments.

    C#
    n=> n<10

    And there's our final lambda expression!

    As you can probably see just by that example, the big advantage of lambda expressions in normal coding is that the syntax is more readable and less verbose. This becomes quickly more important the more complex code becomes. For example, when we just add one more argument, take a look at the difference between the length and readability of an anonymous method vs. a lambda expression:

    C#
    //anonymous method
    numbers.Sort(delegate(int x, int y){ return y-x; });
    
    //lambda expression
    numbers.Sort((x,y)=>  y-x); 

    And in a more complex example, with multiple delegate properties, compare anonymous methods:

    C#
    ControlTrigger trigger= new ControlTrigger ();
    trigger.When=delegate(Control c, ThemePart t)
    { 
       return c.Enabled && c.MouseOver; 
    };
    trigger.Action=delegate(Control c, ThemePart t)
    { 
      t.Visible=true; 
    };
    trigger.ExitAction=delegate(Control c, ThemePart t) 
    { 
       t.Visible=false;
    };

    ...with lambda expressions:

    C#
    ControlTrigger trigger=new ControlTrigger();
    trigger.When=(c,t)=>          c.Enabled && c.MouseOver;
    trigger.Action=(c,t)=>         t.Visible=true;
    trigger.ExitAction=(c,t)=>  t.Visible=false;

    Features and Rules

    Return type and name cannot be specified explicitly (just as with anonymous methods). The return type is always inferred from the delegate signature, and there is no need for a name since the expression is always handled as a delegate.

    You can omit parentheses for the argument list if the expression has one argument:

    C#
    n => n<10

    ...unless its argument has an explicitly-declared data type:

    C#
    //Type explicitly declared for an argument - have to include parentheses!
    (string name)=> "Name: " + name

    If the expression has more than one argument or has no arguments, you must include the parentheses.

    A lambda expression doesn't have to return a value if the signature of the delegate it is being cast to has a return type of void:

    C#
    delegate void EmptyDelegate();
    …
    EmptyDelegate dlgt= ()=> Console.WriteLine("Lambda without return type!");

    The code used in a lambda doesn't have to be a single statement. You can include multiple statements if you enclose them inside a statement block:

    C#
    Action<Control> action= 
       control=>
       {
           control.ForeColor=Color.DarkRed;
           control.BackColor=Color.MistyRose;
       });

    In this form, the lambda more closely resembles an anonymous method, but with a less verbose syntax.

    Lambda statement blocks are not supported by the VS IDE in the LINQ Preview, so they will be underlined as a syntax error. However, they will compile and run correctly in spite of the IDE's lack of support.

    You can access local variables and arguments in the outer method from within the expression, just as you can do with anonymous methods.

    C#
    void GetMatchesFromList(List<int> matchValues)
    {
      List<int> numbers=GetNumbers(); //Get a list of numbers to search in.
      //Get the first number in the numbers list that is also contained in the 
      //matchValues list.
      int result=numbers.Find(n=> matchValues.Contains(n));
    }

    Uses

    Lambda expressions are nifty anywhere you need to pass a little bit of custom code to a component or method. Where anonymous methods were useful in C# 2.0, lambda expressions really shine in C# 3.0.

    Some examples are expressions for filtering, sorting, iterating, converting, and searching lists (using the useful methods introduced in .NET 2.0):

    C#
    List<int> numbers=GetNumbers();
    
    //find the first number in the list that is below 10
    int match=numbers.Find(n=> n<10);
    
    //print all the numbers in the list to the console
    numbers.ForEach(n=> Console.WriteLine(n));
    
    //convert all the numbers in the list to floating-point values
    List<float> floatNumbers=numbers.ConvertAll<float>(n=> (float)n);
    
    //sort the numbers in reverse order
    numbers.Sort((x, y) => y-x);
    
    //filter out all odd numbers
    numbers.RemoveAll(n=> n%2!=0);

    ...progress update handlers passed to a long-running method:

    C#
    metafileConverter.Convert(filename, 
    percentComplete=> progressBar.Value=percentComplete);

    ...and simple event handlers:

    C#
    slider.ValueChanged+= (sender, e)=> label.Text=slider.Value.ToString(); 

    XLinq, MS's new technology for querying XML documents, and Linq, MS's new technology for querying object collections, use lambda expressions heavily.

    How to Use Lambda Expressions

    When the upcoming version of .NET is released, using lambda expressions will be as simple as declaring them in the places you would normally use anonymous methods or delegates:

    C#
    //passing a method as a delegate
    int match=numbers.Find(MatchNumbersUnder10);
    
    //passing an anonymous method as a delegate
    int match=numbers.Find(delegate(int n) { return n<10; });
    
    //passing a lambda expression as a delegate
    int match=numbers.Find(n=>  n<10); 

    Currently, though, you must take some additional steps. The LINQ Preview must be installed on your machine, and you must create a project using one of the LINQ project templates so that VS will know to use the C# 3 compiler included with the LINQ Preview installation.

    Lambda Expression Trees

    There's another powerful feature of lambda expressions that is not obvious at first glance. Lambda expressions can be used as expression trees (hierarchies of objects defining the components of an expression – operators, property access sub-expressions, etc) instead of being directly turned to code. This way, the expressions can be analyzed at runtime.

    To make a lambda expression be treated as an expression tree, assign or cast it to the type Expression<T>, where T is the type of the delegate that defines the expression's signature.

    C#
    Expression<Predicate<int>> expression = n=>  n<10;

    The expression tree created by the expression defined above looks like this:

    Sample image

    As you can see, Expression<T> class has a property called Body, which holds the top level expression object in the expression tree. In the case of the expression above, it is a BinaryExpression with a NodeType of ExpressionType.LT (LessThan). The BinaryExpression object has a Left property that contains the sub-expression to the left of the operator – in this case, a ParameterExpression whose Name property is set to "n". It also has a Right property that contains the sub-expression to the right of the operator – in this case, a ConstantExpression whose value is 10.

    This expression tree can also be created manually like this:

    C#
    Expression<Predicate<int>> expression = Expression.Lambda<Predicate<int>>(
                     Expression.LT(
                              Expression.Parameter(typeof(int), "n"),
                              Expression.Constant(10)
                    ),
                   Expression.Parameter(typeof(int), "n")
       );

    An expression tree can be compiled and turned into a delegate using the Compile() method of the Expression<T>class:

    C#
    //Get a compiled version of the expression, wrapped in a delegate
    Predicate<int> predicate=expression.Compile();
    //use the compiled expression
    bool isMatch=predicate(8); //isMatch will be set to true

    The Compile() method dynamically compiles IL code based on the expression, wraps it in a delegate so that it can be called just like any other delegate, and then returns the delegate.

    Uses

    As shown above, the properties of the expression tree objects can be used to get detailed information about all parts of the expression. This information can be used to translate the expression into another form, extract dependency information, or do other useful things. Microsoft's Database Language-Integrated Query (DLinq) technology, to be introduced in the upcoming version of the .NET Runtime, is based on translation of lambda expressions to SQL at runtime.

    I am working on a component that will allow you to do simple automatic binding via expressions, like this):

    C#
    Binding binding=new Binding<Entry, Label>();
    binding.SourceObject=src;
    binding.DestObject=dest;
    binding.SourceExpression=(src,dest)=> src.Name + 
        " – added on "+src.Date.ToString();
    binding.DestExpression=(dest)=>dest.Text;
    binding.Bind(); 

    The Binding object would analyze the expression tree specified in the SourceExpression property, find all binding dependencies (in this case the Name and Date properties of the source object), attach listeners to their property change events (NameChanged and DateChanged, or else PropertyChanged), and set the property specified in the destination expression when the events are raised to keep the property value of the destination up-to-date.

    Another usage would be a dynamic filter that automatically keeps a list control up-to-date based on changes to the text entered in a filter textbox. All you would have to do to set up the dynamic filter would be:

    C#
    listControl.Filter=(item)=> item.Name.StartsWith(textBox.Text);

    Whenever textbox.Text changes, the filter expression would be run on all the items, and those not matching the filter expression would be hidden.

    Another thing that expression trees are potentially useful for is lightweight dynamic code generation. You can build an expression tree manually as I described above, then call Compile() to create a delegate, and then call it to run the generated code. It is much easier to use expression tree objects to generate code than to try to output the IL manually.

    Conclusion

    As you can see, lambda expressions and expression trees open a lot of new possibilities! I am excited to work with them and to see the uses other developers think of for them.

  • License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    Web Developer
    United States United States
    My main goal as a developer is to improve the way software is designed, and how it interacts with the user. I like designing software best, but I also like coding and documentation. I especially like to work with user interfaces and graphics.

    I have extensive knowledge of the .NET Framework, and like to delve into its internals. I specialize in working with VG.net and MyXaml. I also like to work with ASP.NET, AJAX, and DHTML.

    Comments and Discussions

     
    Questionhow long you been suck the dick Pin
    Member 121615725-Dec-15 15:28
    Member 121615725-Dec-15 15:28 
    QuestionThank You! Pin
    GenadyT10-Mar-14 23:13
    professionalGenadyT10-Mar-14 23:13 
    QuestionVery Nice Pin
    Memon Sufiyan3-Jul-13 22:10
    Memon Sufiyan3-Jul-13 22:10 
    GeneralMy vote of 4 Pin
    ali_sh15-Dec-12 8:52
    ali_sh15-Dec-12 8:52 
    GeneralMy vote of 5 Pin
    aliumer19-Oct-11 4:37
    aliumer19-Oct-11 4:37 
    QuestionAnonymous Method - Memory Leak problem whereas Lambda expression not --> How ? Pin
    babu_208221-Jul-11 0:01
    babu_208221-Jul-11 0:01 
    GeneralMy vote of 5 Pin
    Mayank Kukadia1-Mar-11 22:13
    Mayank Kukadia1-Mar-11 22:13 
    GeneralGreat work Pin
    Sivastyle25-Aug-10 5:17
    Sivastyle25-Aug-10 5:17 
    GeneralMy vote of 5 Pin
    Sivastyle25-Aug-10 5:16
    Sivastyle25-Aug-10 5:16 
    General5 from me Pin
    dybs19-Nov-09 15:57
    dybs19-Nov-09 15:57 
    GeneralGreat introduction Pin
    Option Greek28-Oct-09 4:46
    Option Greek28-Oct-09 4:46 
    GeneralRe: Great introduction Pin
    J. Dunlap9-Nov-09 8:36
    J. Dunlap9-Nov-09 8:36 
    GeneralThank you for the great introduction! Pin
    antecedents25-Jan-08 9:53
    antecedents25-Jan-08 9:53 
    GeneralRe: Thank you for the great introduction! Pin
    J. Dunlap9-Nov-09 8:33
    J. Dunlap9-Nov-09 8:33 
    GeneralOne query over lambda statements Pin
    sreeshti21-May-07 1:51
    sreeshti21-May-07 1:51 
    Generalsmall problem.... Pin
    Jeff Brush7-May-07 11:09
    Jeff Brush7-May-07 11:09 
    GeneralRe: small problem.... Pin
    Stefan Henneken28-May-12 4:15
    Stefan Henneken28-May-12 4:15 
    GeneralNice article. Pin
    Stephen Hewitt28-Mar-07 17:37
    Stephen Hewitt28-Mar-07 17:37 
    GeneralMy 5 as well Pin
    shokisingh22-Mar-07 6:51
    shokisingh22-Mar-07 6:51 
    GeneralDo you know how to do runtime queries using IQuerable Pin
    Sacha Barber16-Mar-07 1:40
    Sacha Barber16-Mar-07 1:40 
    GeneralRe: Do you know how to do runtime queries using IQuerable Pin
    Sander Rossel2-Feb-12 12:03
    professionalSander Rossel2-Feb-12 12:03 
    GeneralRe: Do you know how to do runtime queries using IQuerable Pin
    Sacha Barber2-Feb-12 22:04
    Sacha Barber2-Feb-12 22:04 
    GeneralRe: Do you know how to do runtime queries using IQuerable Pin
    Sander Rossel4-Feb-12 0:21
    professionalSander Rossel4-Feb-12 0:21 
    GeneralGreat article Pin
    Judah Gabriel Himango23-Feb-07 4:35
    sponsorJudah Gabriel Himango23-Feb-07 4:35 
    GeneralVery Cool Pin
    Wes S13-Feb-07 17:21
    Wes S13-Feb-07 17:21 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.