Skip to content

Use captured foreach variable#43357

Merged
sharwell merged 2 commits intodotnet:masterfrom
Therzok:patch-3
Apr 15, 2020
Merged

Use captured foreach variable#43357
sharwell merged 2 commits intodotnet:masterfrom
Therzok:patch-3

Conversation

@Therzok
Copy link
Contributor

@Therzok Therzok commented Apr 14, 2020

Given the behaviour of lambda captures with foreach variables, the code might be hiding a bug.

cc @mavasani @CyrusNajmabadi

@Therzok Therzok requested a review from a team as a code owner April 14, 2020 23:20
@sharwell
Copy link
Contributor

In this case I don't think it makes a difference. The localProject temporary could be removed.

@Therzok
Copy link
Contributor Author

Therzok commented Apr 14, 2020

In this case I don't think it makes a difference. The localProject temporary could be removed.

Yup, you are right! I was mistaking it with the scenario where the variable is declared outside the loop - i.e. for loop index variable.

Removed the redundant local.

@Therzok
Copy link
Contributor Author

Therzok commented Apr 14, 2020

Heh, it seems like the foreach scenario existed, but it was changed in C# 5.0.

@CyrusNajmabadi
Copy link
Contributor

Agreed with @sharwell's assessment. And, even if this was captured, this is safe to do :)

@sharwell sharwell changed the title Use local instead of captured foreach variable Use captured foreach variable Apr 15, 2020
@sharwell sharwell merged commit f3f6268 into dotnet:master Apr 15, 2020
@ghost ghost added this to the Next milestone Apr 15, 2020
@sharwell sharwell modified the milestones: Next, temp, 16.7.P1 Apr 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants