locked
What are Lambda Expressions? RRS feed

Answers

  • Lambda expressions are similar to anonymous methods introduced in C# 2.0, except that lambda expressions are more concise and more flexible.   All lambda expressions use the lambda operator =>, which is read as “goes to”.  The left side of the lambda operator specifies the input parameters and the right side holds the expression or statement block.  Here is an example of lambda expressions:

    delegate int D1(int i);
    D1 myDelegate = x => x + 1;
    int j = myDelegate(5); // j = 6
    
    

     

    Lambda Expression & Anonymous Method

    Let’s demonstrate a lambda expression with an example that starts with a C# 1.0 delegate, then several C # 2.0 anonymous methods and then rewrite it as a lambda expression.

    If we want to find all customers who are based in London, we can use a C# 1.0 delegate as follows.  LoadCustomers is just a function which returns a collection of customers.

    class Customer
    {
        public string CustomerID { get; set; }
        public string CustomerName { get; set; }
        public string City { get; set; }
    }
    static void Main(string[] args)
    {
        List<Customer> customers = LoadCustomers();
        List<Customer> query =
             customers.FindAll(new Predicate<Customer>(CityEqualsLondon));
    }
    static bool CityEqualsLondon(Customer c)
    {
        return c.City.Equals("London");
    }
    

     

    Now let’s use C# 2.0 anonymous methods to write the code.

    static void Main(string[] args)
    {
        List<Customer> customers = LoadCustomers();
        List<Customer> query = 
            customers.FindAll(
            delegate(Customer c) { return c.City.Equals("London"); });
    }
    

     

    Finally, we use a lambda expression in C# 3.0 to rewrite the code.

    static void Main(string[] args)
    {
        List<Customer> customers = LoadCustomers();
        List<Customer> query =
             customers.FindAll(c => c.City.Equals("London"));
    }
    

    We can see that lambda expressions are much more concise.

    For additional information, please see Lambda Expressions (C# Programming Guide).

     

    For more FAQ about Visual C# General, please see Visual C# General FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.



    Tuesday, April 7, 2009 8:59 AM

All replies

  • Lambda expressions are similar to anonymous methods introduced in C# 2.0, except that lambda expressions are more concise and more flexible.   All lambda expressions use the lambda operator =>, which is read as “goes to”.  The left side of the lambda operator specifies the input parameters and the right side holds the expression or statement block.  Here is an example of lambda expressions:

    delegate int D1(int i);
    D1 myDelegate = x => x + 1;
    int j = myDelegate(5); // j = 6
    
    

     

    Lambda Expression & Anonymous Method

    Let’s demonstrate a lambda expression with an example that starts with a C# 1.0 delegate, then several C # 2.0 anonymous methods and then rewrite it as a lambda expression.

    If we want to find all customers who are based in London, we can use a C# 1.0 delegate as follows.  LoadCustomers is just a function which returns a collection of customers.

    class Customer
    {
        public string CustomerID { get; set; }
        public string CustomerName { get; set; }
        public string City { get; set; }
    }
    static void Main(string[] args)
    {
        List<Customer> customers = LoadCustomers();
        List<Customer> query =
             customers.FindAll(new Predicate<Customer>(CityEqualsLondon));
    }
    static bool CityEqualsLondon(Customer c)
    {
        return c.City.Equals("London");
    }
    

     

    Now let’s use C# 2.0 anonymous methods to write the code.

    static void Main(string[] args)
    {
        List<Customer> customers = LoadCustomers();
        List<Customer> query = 
            customers.FindAll(
            delegate(Customer c) { return c.City.Equals("London"); });
    }
    

     

    Finally, we use a lambda expression in C# 3.0 to rewrite the code.

    static void Main(string[] args)
    {
        List<Customer> customers = LoadCustomers();
        List<Customer> query =
             customers.FindAll(c => c.City.Equals("London"));
    }
    

    We can see that lambda expressions are much more concise.

    For additional information, please see Lambda Expressions (C# Programming Guide).

     

    For more FAQ about Visual C# General, please see Visual C# General FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.



    Tuesday, April 7, 2009 8:59 AM
  • http://msdn.microsoft.com/en-us/library/bb397687.aspx
    Lucian Baciu, http://studentclub.ro/lucians_weblog
    Tuesday, April 7, 2009 8:59 AM
  • Is this like a tutorial or something? If so, mark it like a discussion. You do not recieve any points if you answer to your own questions, so I don't see what the point of this thread is.
    Lucian Baciu, http://studentclub.ro/lucians_weblog
    Tuesday, April 7, 2009 9:07 AM
  • Hi naicul,

    Please understand that this thread is one of the C# General FAQs.  

    For more C# General FAQ, please see
    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/2d666562-ed08-4461-bf92-7808913b4e96

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, April 10, 2009 6:02 AM
    Moderator