Lambda Expressions

Lambda is one of those .NET shortcuts that comes in handy as soon as you got used to it.

    class Program

    {

        static void Main()

        {

            //

            // Use implicitly typed lambda expression.

            // … Assign it to a Func instance.

            //

            Func<int, int> func1 = x => x + 1;

            //

            // Use lambda expression with statement body.

            //

            Func<int, int> func2 = x => { return x + 1; };

            //

            // Use formal parameters with expression body.

            //

            Func<int, int> func3 = (int x) => x + 1;

            //

            // Use parameters with a statement body.

            //

            Func<int, int> func4 = (int x) => { return x + 1; };

            //

            // Use multiple parameters.

            //

            Func<int, int, int> func5 = (x, y) => x * y;

            //

            // Use no parameters in a lambda expression.

            //

            Action func6 = () => Console.WriteLine();

            //

            // Use delegate method expression.

            //

            Func<int, int> func7 = delegate(int x) { return x + 1; };

            //

            // Use delegate expression with no parameter list.

            //

            Func<int> func8 = delegate { return 1 + 1; };

            //

            // Invoke each of the lambda expressions and delegates we created.

            // … The methods above are executed.

            //

            Console.WriteLine(func1.Invoke(1));     // 2

            Console.WriteLine(func2.Invoke(1));     // 2

            Console.WriteLine(func3.Invoke(1));     // 2

            Console.WriteLine(func4.Invoke(1));     // 2

            Console.WriteLine(func5.Invoke(2, 2));  // 4

            func6.Invoke();                         //

            Console.WriteLine(func7.Invoke(1));     // 2

            Console.WriteLine(func8.Invoke());      // 2

        }

    }

Source: http://www.dotnetperls.com/lambda

It is an anonymous function without being bound to an identifier. By using it you can contain your methods and handlers in a line of code

var button_Go = new Button { Text = "Go!" };

button_Go.Click += (sender, e) => MessageBox.Show("You clicked Go!");

Button.Click accepts an object and an EventArgs. Knowing this, we can just go ahead and pass these parameters to our anonymous method using the “goes to” parameter

Advertisements

2 thoughts on “Lambda Expressions

  1. Ninjacool says:

    Good read. I miss C#..
    I remember back in the days when Lambda was just a song.

    Ohh, that was Lambada, sorry. 😛

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s