Created
December 29, 2016 12:47
-
-
Save tabakerov/0d7820039db40ad5a70f83327fef831c to your computer and use it in GitHub Desktop.
Local functions and closures in C# 7
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
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