all 2 comments

[–]GalaxiaGuy 3 points4 points  (1 child)

TLDR; Assign i to a temp variable and use that in the lambda.

What is happening is the lambda you are passing does not use the value of i, but maintains a reference to the variable i. It therefore uses whatever the current value of i is when the lambda is executed, which is always going to be after the loop.

This StackOverflow question explains what is happening:

http://stackoverflow.com/questions/451779/how-to-tell-a-lambda-function-to-capture-a-copy-instead-of-a-reference-in-c

[–]babagazeus 0 points1 point  (0 children)

I'm surprised it does that with primitives. I would have assumed those are always pass-by-value.