#1,186 – Capturing a foreach Iteration Variable in a Lambda Expression

If you capture a for loop iteration variable in a lambda expression, the value of the variable that the expression uses will be whatever the final value of the iteration variable is when the loop completes.

Capturing the iteration variable in a foreach loop works differently.  When you capture the iteration variable in a foreach loop, the lambda expression has a copy of the iteration variable with the value that it had at the time that it was captured.

            int[] primes = { 2, 3, 5, 7, 11, 13 };
            List<Action> dels = new List<Action>();

            // Capture foreach iteration variable
            foreach (int p in primes)
                dels.Add(() => Console.WriteLine(p));

            foreach (Action a in dels)
                a();

1186-001

Advertisement

#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

#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