locked
Lambda Expressions RRS feed

  • Question

  • What is Lambda Expressions,can anyone explain me,and how to use it.
    Tuesday, June 9, 2009 12:19 PM

Answers

All replies

  • Hi,

    A lambda expression is like a code block that passed inline to another code block. It's a shortcut syntax for passing functions around. Very similar to anonymous delegates, which was a shortcut to writting a delegate out full. 

    =========================
    In 2003 developers wrote code like this..... 

    delegate sortingDelegate = SortingAlgorithm
    array.sort(sortingDelegate)


    public void SortingAlgorthim()
       //algorithm in here
    end void

    =========================

    In 2005 developers wrote code like this, anonymous functions

    array.sort(delegate {
          //algorithm in here
     })

    =========================

    In 2008 developers can write code like this, lambda expressions

    array.sort(item => //algorithim here)

    =========================

    The code is getting shorter which is excellent because of LINQ.

    LINQ which uses lambda expressions would have been very messy without them.

    So a lambda expression is like a function / procedure / method that can be written in a very short syntax and then passed to another function / procedure / method. They are there to make LINQ much more readable as well as a few other advantages.
    www.dsmyth.net | www.dsmyth.net/wiki
    Tuesday, June 9, 2009 12:31 PM
  • The explanation was given Derek already.
    Let me show you some examples:

    Ex01:
     class Ex01_LambdaExpressions
        {
            delegate int del(int i);

            static void Main(string[] args)
            {
                del d = x => x * x;
                int a = d(5); //a = 25
                Console.WriteLine(a);
               
            }
        }

    Ex02:
    class Ex02_LambdaExpressions
        {
            delegate T F<T>(T t);

            static void Main(string[] args)
            {
                // Anonymous Method
                F<int> d = delegate(int x){ return x + x; };


                // Lambda Expression
                F<int> l = x => x + x;

                //Will produce the same output
                Console.WriteLine(d(5));
                Console.WriteLine(l(5));


            }
        }



    Bhaskar MCPD:WinForms http://vsxperts.blogspot.com
    Tuesday, June 9, 2009 5:45 PM
  • I wrote a blog on this a while back intended to help developers that understand delegates understand lambda expressions.  You can find it here:

    http://blog.davemorton.net/2008/12/understanding-lambda-expressions.html
    David Morton - http://blog.davemorton.net/ - @davidmmorton
    • Marked as answer by Bin-ze Zhao Friday, June 12, 2009 8:24 AM
    Tuesday, June 9, 2009 5:47 PM
    Moderator
  • I did a talk on this at TechEd. You can download the sample code I used for TechEd here if you want more sample code:

    http://www.insteptech.com/speaking/speaking.htm
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Bin-ze Zhao Friday, June 12, 2009 8:25 AM
    Tuesday, June 9, 2009 5:56 PM
  • Ok, let me add my shameless plug. ;-)

    Here is a real world example pulled from these here forums which show two Lambda's in action!

    C#: Splitting Data From a String and Extracting out Decimals and Integers into Separate Lists Using Extension Methods
    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by Bin-ze Zhao Friday, June 12, 2009 8:25 AM
    Tuesday, June 9, 2009 6:33 PM
    Moderator
  • Bummer, I don't have a blog, or an invitation to speak in public (shudder), or even any examples.. I guess I'm spending my free time all wrong.
    Tuesday, June 9, 2009 6:41 PM
  • Bummer, I don't have a blog, or an invitation to speak in public (shudder), or even any examples.. I guess I'm spending my free time all wrong.

    Part of the reason I started my blog was to answer questions in the forums. I got tired of answering the same question over and over by either cut and paste or having to redo. Providing a link to a thought out article is priceless and a true time saver.

    William Wegerson (www.OmegaCoder.Com)
    Tuesday, June 9, 2009 6:57 PM
    Moderator
  • Hmm.. That's a really good idea. I might have to take up blogging.. someday.
    Tuesday, June 9, 2009 7:00 PM