How ILSpy reconstructs a labeled C# loop

ProjectsCommunity Digest0 replies

Community DigestMember#1

In C# 15 Unpacked: Popping the Hood on Labeled Loops and Finding No Labels at All, Giorgi Kobaidze opens a Release build of a nested order-processing loop in ILSpy. The original C# names the outer loop with a label. The decompiled C# expresses the same two paths using an indexed inner loop and an ordinary break.

Trace both exits from the inner loop

The outer loop visits orders while the inner loop visits the items in one order. When the inner loop encounters an out-of-stock item, the source uses a labeled continue to advance the outer loop. This skips the remaining items and the final shipping message for that order. A completed scan reaches the shipping message before the next order begins. Microsoft's C# jump statement reference documents that a labeled continue in C# 15 targets an enclosing loop.

In the author's ILSpy output, an ordinary break exits the inner while loop as soon as an item is missing. The surrounding order body ends immediately after that loop, so the next order begins. The successful path reaches the array bounds check, prints the shipping message when the scan finishes, and then also breaks out. Where the success message sits explains how both paths can share an exit while producing different output.

Review observable behavior first

This example gives reviewers two cases to trace when the decompiled loop looks unfamiliar. An order with a missing item followed by another item should skip that later item and omit the shipping message. An order whose items are all present should reach the shipping message once. Including both orders in the same run also checks that processing moves on after a missing item.

The article shows one Release build and one decompiler presentation. The branch targets in the compiled method describe the control flow, and the decompiler chooses C# constructs to express it. That distinction matters when tracing code from a library assembly. A reconstructed loop may answer which statements execute on each path while leaving the original author's choice of syntax unknown.

No replies yet