Skip to content

Instantly share code, notes, and snippets.

@plioi
Created March 7, 2012 00:46
Show Gist options
  • Save plioi/1990154 to your computer and use it in GitHub Desktop.
Save plioi/1990154 to your computer and use it in GitHub Desktop.
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