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/

About The Author

I am a software developer from Poland, currently focused on the .NET platform. I’ve been incredibly passionate about IT since a young age and am always seeking to expand my skillset. Furthermore, my personal development plan includes machine learning, cryptocurrency, image processing, and the Scrum framework. Turning to the personal part of my life. I’m a licensed paraglider holding an International Pilot Proficiency Information Card, a proud Arsenal F.C. supporter and avid traveller with an infatuation for the natural beauty of New Zealand. I’m keen on unusual or extreme sports, and I love to discover and try out new things.

Related Posts

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.