Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When did `foreach` stop working on strings?

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... :(

like image 402
Tim Long Avatar asked Sep 10 '15 17:09

Tim Long


1 Answers

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.

like image 151
Tim Long Avatar answered Oct 30 '22 15:10

Tim Long