Skip to content

Instantly share code, notes, and snippets.

@pragmaticlogic
Created December 6, 2013 07:29
Show Gist options
  • Save pragmaticlogic/7819916 to your computer and use it in GitHub Desktop.
Save pragmaticlogic/7819916 to your computer and use it in GitHub Desktop.
Closure in C# - a
public class Program
{
private static Func<int>[] GetFunc()
{
int SIZE = 5;
Func<int>[] func = new Func<int>[SIZE];
for (int i = 0; i < SIZE; i++)
{
func[i] = () => i;
}
return func;
}
public static void Main(string[] args)
{
var funcs = Program.GetFunc();
foreach (Func<int> func in funcs)
{
Console.Out.WriteLine(func());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment