In certain situations with a for-loop nested inside another loop, a break-statement in the inner loop will decompile to a goto that also exits the outer loop.
If this code is compiled in RELEASE mode:
public void NestedForTest(IEnumerable<char> line)
{
foreach (var ch in line)
{
for (int i = 0; i < 4; i++)
{
Console.Write("-"); // If this line is removed it works
if (ch == 't') break;
Console.Write(ch);
}
}
Console.WriteLine();
}
The following code is decompiled:
public void NestedForTest(IEnumerable<char> line)
{
Label0:
foreach (char ch in line)
{
for (int i = 0; i < 4; i++)
{
Console.Write("-");
if (ch == 't')
{
goto Label0; // THIS IS WRONG!
}
Console.Write(ch);
}
}
Console.WriteLine();
}
NOTE:
- It is only a problem when the code is compiled in a RELEASE configuration (in DEBUG it interpret it as a while-loop and the code is correct)
- If the line before the exit-check is removed (see comment in code) it decompiles correctly.