Bug 3847 - Peculiar foreach behaviour with regard to closures.
Summary: Peculiar foreach behaviour with regard to closures.
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC All
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2012-03-11 10:24 UTC by Daniil Frumin
Modified: 2012-03-13 06:27 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
example (319 bytes, text/plain)
2012-03-11 10:25 UTC, Daniil Frumin
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Daniil Frumin 2012-03-11 10:24:14 UTC
The attached file, when compiled under the Mono C# compiler version 2.10.5.0, produces the following output:
3
3
3

While the result indeed mimics .NET's behaviour it is clear that there was a design error (at least according to people on SO), since it is expected for closures in that case to enclose on _different_ variables. Microsoft has recently changed the implementation of foreach and the behaviour is no longer perplexing in VS11.

I do understand that patching foreach might break some backward compatibility in theory, but in practice I can't honestly find a usecase for this (e.g.: when you need all of your closures to be enclosed on one variable with the last value).

Please pardon my English as I am not a native speaker.
Thanks.
Comment 1 Daniil Frumin 2012-03-11 10:25:22 UTC
Created attachment 1494 [details]
example
Comment 2 Marek Safar 2012-03-13 06:27:47 UTC
Fixed in master