All the code in this series is available on GitHub. Clone it, run it, and try breaking it yourself.

GitHub logo georgekobaidze / dotnet11-playground

A lightweight playground repository for experimenting with and testing new features introduced in .NET 11 and C# 15.

.NET 11 & C# 15 Playground

.NET 11 & C# 15 Playground

A lightweight playground repository for experimenting with and testing new features introduced in .NET 11 and C# 15.

Use it to explore language changes, try out APIs, and validate ideas in isolation without production constraints. Each feature lives in its own small project, often with a "before" and "after" version so you can see what the new feature actually changes.

What's inside

Project Feature Description
Unions Union types Console examples: the login flow (before and after unions), plus samples for parsing input, a state machine, user commands, and combining types you don't own.
UnionsWebApi Union types A minimal API that returns a union from a service and maps each outcome to an HTTP response.

More examples will be added as new features are explored.

Getting started

You'll need the .NET 11 SDK. Then clone the repo and run any example:

dotnet run
…

Follow me on GitHub for more updates!

Table of Contents

  • Where We Left Off
  • Time to Pop the Hood
  • The Decompiler Has Its Own Ideas
  • Decoding the Trick
  • Bonus: Two Compiler Tricks Hiding in Plain Sight
  • So What Does This Mean?
  • Wrapping Up

Where We Left Off

In my previous article, we looked at labeled break and continue, the retro new feature in C# 15 that Java has had since 1996. We used it to skip an entire order the moment we found an out-of-stock item:

nextOrder: foreach (string[] order in orders) { Console.WriteLine("New order:"); foreach (string item in order) { if (item == "OUT_OF_STOCK") { Console.WriteLine(" Item missing, skipping this order."); continue nextOrder; // jump to the next ORDER, not the next item } Console.WriteLine($" Packing {item}"); } Console.WriteLine(" Order shipped!"); } 

Clean, readable, and no bool flags.

Time to Pop the Hood

Every new C# feature is a promise from the compiler: "write it this way, and I'll take care of the rest." And every time, I want to see what "the rest" actually looks like.

Here's a quick refresher on what happens to your C# code:

  1. The C# compiler turns your code into IL (Intermediate Language) and stores it in a DLL.
  2. At runtime, the JIT compiler turns that IL into machine code for your CPU, and that's what actually runs.

The direction is always one way: C# → IL → machine code. Your original C# source never ends up in the DLL.

That's where labels get interesting. IL has no labels, no loops, no break, and no continue. It only has branch instructions that jump to offsets. So by the time your code is compiled, the labels are gone.

So how can a tool like ILSpy show us C# at all? It doesn't have our source code. It reads the DLL and rebuilds C# from what's inside:

  • The metadata tells it the names of types, methods, and their signatures, so those come back exactly right.
  • The IL tells it what each method does, as a flat list of instructions and jumps. ILSpy has to figure out which loops, ifs, and breaks would produce those jumps.

Some things can't be recovered at all. Local variable names, for example, aren't stored in the IL, so ILSpy invents its own.

In other words, decompiled C# isn't your code. It's a reconstruction: C# that compiles to equivalent IL. And that raises a fun question: if the labels are gone, what does ILSpy rebuild when we hand it our labeled loop?

The Decompiler Has Its Own Ideas

The answer turned out to be more interesting than I expected. ILSpy didn't give me back my original code. It restructured the inner loop into something I never wrote, and it's a pretty clever trick.

I built the project in Release mode, opened the DLL in ILSpy, and asked it to decompile the Program class back into C#. Here's what came out:

Decompiled C#

Same behavior, but a completely different shape. My nextOrder: label is gone. My inner foreach is gone. And my continue nextOrder; turned into a plain, unlabeled break.

Decoding the Trick

At first glance, this looks wrong. Where did the continue nextOrder; go? And why is "Order shipped!" now inside a loop?

Here's what ILSpy did:

My code ILSpy's version
Inner foreach (string item in order) A while (true) loop with a manual index (num) and a bounds check
continue nextOrder; break; out of the while (true)
Moving to the next item num++; followed by continue;
Inner loop finishing normally The bounds check fails, then "Order shipped!", then break;

The key is what comes after the while (true) loop: nothing. The outer foreach body ends right there. So breaking out of the inner loop drops you straight into the next iteration of the outer loop, which is exactly what continue nextOrder; did.

The "Order shipped!" line moved inside the loop so it only runs on the "all items packed" path. The out-of-stock path breaks out before reaching it.

ILSpy took a labeled jump across two loops and turned it into a plain break out of a single loop. It's clever, and it's definitely not code a human would write.

Bonus: Two Compiler Tricks Hiding in Plain Sight

While we're here, the decompiled code reveals two things the C# compiler does with every foreach over an array, labeled or not:

  • There's no enumerator. A foreach over an array doesn't call GetEnumerator(). The compiler turns it into a plain index-based loop, because that's faster. That's where num and array3.Length come from.
  • Hidden temporary variables. array2 = array and array3 = obj are extra locals the compiler introduces for foreach over arrays. They copy the reference, not the array itself. You never wrote them, but they're part of how the loop is lowered.

None of this is new in C# 15, but it's a nice reminder of how much the compiler rewrites behind the scenes, even for the simplest loops.

So What Does This Mean?

Let's put it all together.

The label is a compile-time thing only. nextOrder: helps you say which loop you mean, and it helps the compiler check that you're only jumping to a loop you're actually inside. Once the compiler has done that check, the label has nothing left to do, so it doesn't make it into the IL at all.

There's no runtime cost. The entire continue nextOrder; becomes a single jump instruction. The old bool flag approach would need an extra local variable, an assignment, and an extra check after the inner loop. The labeled version needs one jump, and the JIT just sees a branch like any other.

And at the IL level, everything is a jump. In the previous article, I asked whether labeled loops are just goto in a nicer suit. After popping the hood, the honest answer is that in IL, everything is goto. Your for loops, your while loops, your if statements, your break and continue: they all end up as br, brtrue, brfalse, and blt. The difference between goto and a labeled continue isn't in the IL. It's in what the compiler lets you do before it gets there.

goto lets you jump almost anywhere. A labeled continue only lets you jump to the next iteration of a loop you're inside. Same instruction at the end, very different rules on the way in.

Wrapping Up

Here's what we found under the hood:

  • The decompiled C# doesn't look like the original. ILSpy rebuilt our inner loop as a while (true) and turned continue nextOrder; into a plain break, because decompiled code is a reconstruction, not our source.
  • continue nextOrder; compiles to a single jump instruction. IL has no loops or labels, only branches, so the whole feature boils down to one jump to the right place.
  • Labels don't exist at runtime. They're a compile-time tool for readability and safety, with zero runtime cost.

So, the feature named after labels doesn't have a single label left after compilation. Kind of poetic, isn't it?

All the code from this article is in my .NET 11 playground: dotnet11-playground. Clone it, open it in ILSpy, and see for yourself.

Have you ever opened your own code in a decompiler and barely recognized it? Tell me about it in the comments. 👇

Let's stay connected!