It is the turn of programming riddle number 2. Now, it is dedicated to developers who coding in C#.
So, the question is – what is the output of the following code:
var actions = new List<Func<int>>();
var iterator = 0;
while (iterator < 10)
{
actions.Add(() => iterator);
++iterator;
}
foreach (var act in actions)
{
Console.WriteLine(act.Invoke());
}
I know that is an old and tricky ‘feature’ from Microsoft but it often raises during to job interview so I decided to remind everyone who has not heard about it before.
Solution for this riddle you can find at Microsoft blog:
https://blogs.msdn.microsoft.com/abhinaba/2005/10/18/c-anonymous-methods-are-not-closures/
Leave a Reply