Skip to content

Instantly share code, notes, and snippets.

@tabakerov
Created December 29, 2016 12:47
Show Gist options
  • Save tabakerov/0d7820039db40ad5a70f83327fef831c to your computer and use it in GitHub Desktop.
Save tabakerov/0d7820039db40ad5a70f83327fef831c to your computer and use it in GitHub Desktop.
Local functions and closures in C# 7
using System;
class Program
{
static Func<int, int> CreateAdder(int a)
{
int F(int i)
{
Console.WriteLine($"Closured {a}");
return a + i;
}
return F;
}
static void Main(string[] args)
{
var add1 = CreateAdder(1);
var d = add1(4);
Console.WriteLine(d);
var add2 = CreateAdder(2);
d = add2(4);
Console.WriteLine(d);
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment