#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

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #1,180 – Lambda Expressions Can Modify Captured Variables

  1. Pingback: Dew Drop – September 11, 2014 (#1853) | Morning Dew

Leave a comment