Skip to content

Action<IContainer> strange behavior #773

Discussion options

You must be logged in to vote

The problem is related to the scope of the item variable.

You have declared this variable before the for loop, so it exists as a single reference and is updated by each loop iteration. Lambda expression captures this variable, but as declared in the outer scope, it will store only the last item value from a lambda perspective.

However, if you put your variable inside a loop, its scope will differ, and a separate reference will be created for each loop iteration. Therefore, your lambda will capture the correct expected value (and you will likely get a warning about capturing variables and why it may introduce some unexpected pressure on GC).

You have posted a screenshot instead of code, so…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Guilherme-Cassel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants