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