Home Dashboard Directory Help
Search

C# 5 compiler doesn't respect the semantics of the foreach range variable when langversion < 5 by Thomas Levesque


Status: 

Active


1
0
Sign in
to vote
Type: Bug
ID: 732657
Opened: 3/22/2012 2:14:20 PM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

In C# 5, a change has been made to the semantics of the range variable in foreach loops : the range variable is now logically inside the loop, rather than outside. This change is a good thing, since it avoids a common mistake when capturing the range variable in a lambda expression, however I noticed that when targeting an earlier version of C# (using the /langversion switch), the new behavior is still applied. I think this is incorrect, because it means you can't revert to the old behavior when you compile older code that depends on it. This is a breaking change ! I know it is a deliberate breaking change, but it doesn't have to be : we should still be able to compile with an older language version and get the same behavior as before (if not, what's the point of the /langversion switch ?)
Details
Sign in to post a comment.
Posted by Microsoft on 4/18/2013 at 6:00 PM
Thanks for reporting this issue you've encountered with Visual Studio!

As you discovered, this behavior of /langversion is by design. The /langversion switch only blocks off newer constructs through the parser - it does not condition the semantics of the language by attempting to reproduce old bugs and behavior.

Alex Turner
Senior Program Manager
Visual Basic and C# Compiler
Posted by Thomas Levesque on 4/4/2013 at 8:12 AM
Thanks to former Microsoft employee Eric Lippert (see this article: http://ericlippert.com/2013/04/04/what-does-the-langversion-switch-do/), I now realize that I had completely misunderstood the meaning of the langversion switch... So it's not a bug eventually, the compiler is behaving as it should; this issue can be closed.
Posted by MS-Moderator08 [Feedback Moderator] on 3/22/2012 at 7:32 PM
Thank you for submitting feedback on Visual Studio 11 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.

Posted by MS-Moderator01 on 3/22/2012 at 6:54 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.