#1,182 – Capturing a for Loop Variable in a Lambda Expression

If you capture a variable in a lambda expression that is declared within the initializer of a for loop, the results may not be what you expect.  Recall that the value of a captured variable is evaluated when a delegate is invoked, rather than when it is assigned.  If you therefore invoke the delegate after all iterations of the loop have executed, the value of the captured variable will be whatever the final value of the variable was.

Below, notice that the same value is displayed during each invocation of the delegate.

            Action[] dels = new Action[3];

            for (int i = 0; i < 3; i++)
                dels[i] = () => Console.WriteLine(i + 1);

            // Prints 4/4/4, rather than 1/2/3
            foreach (Action d in dels)
                d();

1182-001

Advertisements

#1,181 – Instantiating an Object within a Lambda Expression

You can create instance of objects within lambda expression.  You can also optionally return that instance as the result of the expression.

            // Delegate set to dog-creating lambda
            Func<string,Dog> dogCreator = s => {
                Dog d = new Dog(s);
                d.Bark();
                return d;
            };

            Dog d1 = dogCreator("Lassie");

            Thread.Sleep(3000);  // wait 3 secs

            Dog d2 = dogCreator("Kirby");

            // Dump out creation time info
            Console.WriteLine(d1);
            Console.WriteLine(d2);

1181-001

#1,180 – Lambda Expressions Can Modify Captured Variables

When a variable is captured by inclusion in a lambda expression, the expression is free to modify the value of the captured variable, assuming that the variable is modifiable in the scope in which the lambda is defined.

            int localVariable = 10;

            Action<int> adder = i => localVariable += i;

            Console.WriteLine(string.Format("local at start: {0}", localVariable));
            adder(5);
            Console.WriteLine(string.Format("after calling adder, local: {0}", localVariable));

1180-001

#1,179 – Captured Variable’s Lifetime Matches Delegate

When a variable is captured by inclusion in a lambda expression, the lifetime of that variable becomes the lifetime of the delegate that the lambda is assigned to.

In the example below, we have a local variable whose scope is the body of the SomeFunction method.  We then use this local variable in a lambda expression that is assigned to a delegate instance.  After we exit SomeFunction, we can invoke the delegate and see that the variable defined in SomeFunction is still accessible.

        static Action aDelegate;

        static void Main(string[] args)
        {
            SomeFunction();

            // After call, invoke delegate to show
            // that magicNum is still alive
            aDelegate();

            Console.ReadLine();
        }

        static void SomeFunction()
        {
            int magicNum = 42;

            // Assign delegate to print value of local
            // variable
            aDelegate = () => Console.WriteLine(magicNum);
        }

1179-001

#1,178 – Captured Variables Are Evaluted when a Delegate Is Invoked

A captured variable is a variable declared outside of the scope of a lambda expression, but used within the expression.

            int magicNum = 20;
            Func<int, int> mulByMagic = i => i * magicNum;

            Console.WriteLine(mulByMagic(10));  // 200

Note that the value used for a captured variable is whatever value is current when the associated delegate is invoked, rather than the value that the variable had when the lambda was defined.

            int magicNum = 20;
            Func<int, int> mulByMagic = i => i * magicNum;

            Console.WriteLine(mulByMagic(10));  // 200

            magicNum = 100;
            Console.WriteLine(mulByMagic(10));  // 1000

#1,177 – Lambda Expressions Can Reference Variables Declared Outside of Expression

A lambda expression can make use of its own parameters and local variable declarations.  For example:

            Action<int> act1 = (num) =>
            {
                for (int i = num; i < num + 2; i++)
                {
                    int result = i * 2 - 1;
                    Console.WriteLine(string.Format("{0}, {1}", i, result));
                }
            };

A lambda expression can also make use of a local variable or parameter declared outside of the lambda expression. For example:

        static void Main(string[] args)
        {
            DoSomething(4);
            Console.ReadLine();
        }

        static void DoSomething(int someParam)
        {
            int magicNum = 2;

            Action<int> act1 = (num) =>
            {
                for (int i = num; i < num + (magicNum * someParam); i++)
                {
                    int result = i * 2 - 1;
                    Console.WriteLine(string.Format("{0}, {1}", i, result));
                }
            };

            act1(5);
        }

1177-001

#1,176 – How an Expression Tree is Stored in Memory

You can assign a lambda expression to an expression tree, storing the expression in an instance of Expression<TDelegate> (e.g. Expression<Func<double,double>>).

Suppose that we store the expression to convert from fahrenheit to celsius:

            // Assign to expression tree
            Expression<Func<double, double>> fahrToCelsius =
                (f) => (f - 32.0) * 5.0 / 9.0;

We can now look at this element in memory, to see how this expression is stored. Expression<T> has a Body property that is of type Expression and represents the top-level of the expression tree.  The Expression’s NodeType property indicates the operator used in the expression.  In this example, the Body will be of type BinaryExpression, which has Left and Right properties which are also of type Expression and represent left and right sub-expressions.

This is shown below by examining the expression tree in the debugger for the fahrenheit to celsius expression shown above.

1176-001