Created
March 7, 2012 00:46
-
-
Save plioi/1990154 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
public class Environment | |
{ | |
public string name; | |
//As well as one field for each of the | |
//original loop's local variables, if | |
//there were any... | |
} | |
public class Greeter | |
{ | |
private readonly Environment _environment; | |
public Greeter(Environment environment) | |
{ | |
_environment = environment; | |
} | |
public string Execute() | |
{ | |
return "Hello, " + _environment.name + "!"; | |
} | |
} | |
static void Main(string[] args) | |
{ | |
var delayedGreetings = new List<Greeter>(); | |
{ | |
using (var e = ((IEnumerable<string>)new[] { "Larry", "Moe", "Curly" }).GetEnumerator()) | |
{ | |
var environment = new Environment(); | |
while (e.MoveNext()) | |
{ | |
environment.name = e.Current; | |
delayedGreetings.Add(new Greeter(environment)); | |
} | |
} | |
} | |
//Some time later... | |
foreach (var greeting in delayedGreetings) | |
Console.WriteLine(greeting.Execute()); | |
Console.ReadLine(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment