Created
December 11, 2014 04:16
-
-
Save ilovejs/0289b5ab0e26820d5c3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| namespace VariableCapture | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var actions = new List<Action>(); | |
| //The initialization part logically only happens once, so it's entirely logical that there's only one "variable instantiation". | |
| for (int i = 0, j = 10; i < j; i++) | |
| { | |
| var k = i; | |
| //lazy function, j evaluate on runtime. | |
| actions.Add(() => Console.WriteLine("{0},{1}", k, j)); | |
| } | |
| foreach (var action in actions) | |
| { | |
| action(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment