Why is it bad to use an iteration variable in a lambda expression

Consider this code:

List<Action> actions = new List<Action>();

for (int i = 0; i < 10; i++)
{
    actions.Add(() => Console.WriteLine(i));
}

foreach (Action action in actions)
{
    action();
}

What would you expect this to print? The obvious answer is 0…9 – but actually it prints 10, ten times. It’s because there’s just one variable which is captured by all the delegates. It’s this kind of behaviour which is unexpected.

EDIT: I’ve just seen that you’re talking about VB.NET rather than C#. I believe VB.NET has even more complicated rules, due to the way variables maintain their values across iterations. This post by Jared Parsons gives some information about the kind of difficulties involved – although it’s back from 2007, so the actual behaviour may have changed since then.

Leave a Comment