#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

Advertisement

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 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 )

Connecting to %s

%d bloggers like this: