3 ms·
OP misunderstood but you have it right. C# did not change the behavior of "for" loops; they only changed "foreach" loops. As an example: List<Action> list
by electroly 3y ago
OP misunderstood but you have it right. C# did not change the behavior of "for" loops; they only changed "foreach" loops. As an example:
List<Action> list = new();
for (var i = 0; i < 10; i++)
list.Add(() => Console.WriteLine(i));
foreach (var action in list)
action();
This (still) prints all 10s. All of the closures are referencing the same "i".
- tapirl 3y agoI don't misunderstand it. I know the facts in C#. But the Go proposal is different. The proposed change is for both "for;;" loops and "for-range" loops.
- electroly 3y agoI was referring to the HN poster "Someone" at the beginning of this subthread, my apologies for the confusion--I didn't want to name them explicitly in my post since that feels a little aggressive. They said C# 5 made this change, which is wrong; that's why I explained with a C# snippet. This subthread isn't referring to your post at all.