using System;
public class Test
{
public static Func<int> GeneraDelegado()
{
int variableLocal = 0;
return delegate () { return variableLocal++;}; // Retorna un closure
}
public static void Main()
{
Func<int> natural = GeneraDelegado();
Console.WriteLine (natural()); // 0
Console.WriteLine (natural()); // 1
}
}
dXNpbmcgU3lzdGVtOwoKcHVibGljIGNsYXNzIFRlc3QKewoJcHVibGljIHN0YXRpYyBGdW5jPGludD4gR2VuZXJhRGVsZWdhZG8oKQoJewoJICAgIGludCB2YXJpYWJsZUxvY2FsID0gMDsKCSAgICByZXR1cm4gZGVsZWdhdGUgKCkgeyByZXR1cm4gdmFyaWFibGVMb2NhbCsrO307ICAgIC8vIFJldG9ybmEgdW4gY2xvc3VyZQoJfQoKCXB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKCkKCXsKCSAgICBGdW5jPGludD4gbmF0dXJhbCA9IEdlbmVyYURlbGVnYWRvKCk7CgkKCSAgICBDb25zb2xlLldyaXRlTGluZSAobmF0dXJhbCgpKTsJLy8gMAoJICAgIENvbnNvbGUuV3JpdGVMaW5lIChuYXR1cmFsKCkpOwkvLyAxCgl9Cn0=