Skip to content

Instantly share code, notes, and snippets.

@Mr-Byte
Created January 23, 2013 15:58
Show Gist options
  • Save Mr-Byte/4608614 to your computer and use it in GitHub Desktop.
Save Mr-Byte/4608614 to your computer and use it in GitHub Desktop.
C# 5: Works as one would reason. C# 4 and below: All eight planets are Neptune?! This is why C# 5 is the superior version.
using System;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var planets = new[] {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
var planetPrinters = new List<Action>();
foreach (var planet in planets)
{
planetPrinters.Add(() => Console.WriteLine(planet));
}
foreach (var printer in planetPrinters)
{
printer();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment