fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. int[] items = {1, 2, 3, 4, 5};
  9. List<Action> actions = new List<Action>();
  10.  
  11. foreach(int i in items) {
  12. int j = i; // Reassignment because of how foreach works, but this
  13. // is still part of block scope; it's not a closure.
  14.  
  15. actions.Add(() => Console.WriteLine(j.ToString()));
  16. }
  17.  
  18. foreach(Action a in actions) a();
  19. }
  20. }
Success #stdin #stdout 0.04s 37048KB
stdin
Standard input is empty
stdout
1
2
3
4
5