#1,180 – Lambda Expressions Can Modify Captured Variables
September 11, 2014 1 Comment
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));