Skip to content

Instantly share code, notes, and snippets.

@ilovejs
Created December 11, 2014 04:16
Show Gist options
  • Save ilovejs/0289b5ab0e26820d5c3f to your computer and use it in GitHub Desktop.
Save ilovejs/0289b5ab0e26820d5c3f to your computer and use it in GitHub Desktop.
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