When did `foreach` stop working on strings?
Asked Answered
P

1

8

I have a number of projects that target .NET Micro Framework 4.3.2. I developed them in Visual Studio 2013. I've just upgraded to Visual Studio 2015, and now I'm getting odd problems with the same code that used to compile and run.

Specifically, I've started getting this error:

CS1579 foreach statement cannot operate on variables of type 'string' because 'string' does not contain a public definition for 'GetEnumerator'

And fair enough, it doesn't seem to. But this worked when I developed the code. It is compiled, pushed to NuGet and working in several other projects, so it MUST have worked.

So, why did this stop working in Visual Studio 2015? I have many projects that use this idiom to iterate through the characters in a string, I hope I'm not going to have to change them all... :(

Pound answered 10/9, 2015 at 17:10 Comment(13)
Can't seem to reproduce in a regular .NET or .NET for Windows Store apps project with the new compiler and everything. Could be a Micro Framework issue after all.Scrutator
It certainly does work: rextester.com/BQUY75614 So, this might be a change in c#6Marijo
It works fine for me in VS 2015 with .NET Framework 4, 4.5, and 4.6. This must be a micro framework issue.Misshape
Are you sure about using the same Fx version, 4.3.2, in both Visual studio's?Narcosis
@HenkHolterman yes positive. I'm using Netduino.IP and there is only one release available. It works in VS2013 but not 2015.Pound
@Scrutator ...but NetMF hasn't changed, only Visual Studio. I've also raised this on Netduino forums to see if they know anything there.Pound
I suggest putting it on Connect as well. Both the String class and the GetEnumerator stuff are getting a lot of special treatment from the compiler. This seems to be an issue in combination with the micro framework.Narcosis
@HenkHolterman advice taken: connect.microsoft.com/VisualStudio/feedback/details/1775495/…Pound
@Closers: The minimal verifiable example is 1 line and well described here. You just need to install the μ framework.Narcosis
Is it just me, or are the SO moderators getting more and more 'right wing' ?Pound
I received a bone-headed reply from Microsoft: "I just copied the code and then compiled it in VS 2013 Update 5. But I am unable to repro it. What is .NET Micro Framework 4.3.2 ? Is it .NET Micro Framework 4.5.2 ? If possible could you please give me your detailed repro steps with some screenshots? Please submit this information to us within 4 business days." I would love to, but the issue has vanished from Connect! Argggggghhh.Pound
I can still see it at Connect. Better post a patient reply there. And you do need to be very explicit about platform, required SDK's etc. I don't have the μFx here so I can't research.Narcosis
If you're using the C#6 compiler, you might also want to try and post here: github.com/dotnet/roslyn/issues.Domiciliate
P
1

The C# compiler pre-Roslyn had special treatment for strings and implementing IEnumerable wasn't required. The NetMF implementation of string therefore doesn't implement IEnumerable and pre-Roslyn compilers were happy with that. Roslyn apparently lacked this special treatment of strings and so broke backward compatibility. This was a breaking change in the Roslyn compiler. The issue was migrated to GitHub here: github.com/dotnet/roslyn/issues/11387

A patch was merged and the issue is now closed.

Pound answered 1/8, 2019 at 0:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.