After writing a code with threads inside "for" loop, somehow the incremented value continues after the statement is false. I even try to double check it... How is that even possible?
Asked
Active
Viewed 34 times
0
-
3Please post code, not screenshots of code. But I can tell you this is nothing to do with .NET6 – Jamiec Jan 28 '22 at 16:05
-
4The bug is in your code, not in .NET. Basically the *variable* `i` is captured, not the value in the loop. I'll find a duplicate... – Jon Skeet Jan 28 '22 at 16:11
-
1See also: https://stackoverflow.com/questions/8116709 (and many others) – Jon Skeet Jan 28 '22 at 16:12
-
Many many others, like [Why is it bad to use an iteration variable in a lambda expression](https://stackoverflow.com/questions/227820/why-is-it-bad-to-use-an-iteration-variable-in-a-lambda-expression) and [Captured variable in a loop in C#](https://stackoverflow.com/questions/271440/captured-variable-in-a-loop-in-c-sharp) – Wyck Jan 28 '22 at 16:13