A switch expression reads like a lookup table: a value goes in, one of several results comes out, and the arms sit in a tidy column. But the CLR has no "match" instruction and no concept of an expression that chooses between arms. By the time the compiler is done, that tidy column is either a single switch opcode pointing at a jump table, or a balanced forest of integer compares — and nothing in the metadata records which syntax you wrote. Reading it back means recognizing two very different lowerings as the same construct, and finding the one instruction that proves it was an expression and not a statement.
The real question
The switch IL opcode is not exotic: it pops an integer, and if that integer is in [0, n) it branches to the n-th target in its operand list, otherwise it falls through. Decoding the instruction is trivial. The hard part is structural. The compiler picks between a jump table and a comparison tree based purely on how dense the case values are, so Glass has to recognize both shapes as a switch, recover the real case value behind a normalized offset, and then decide whether what it is looking at was a switch expression that yields a value or a switch statement that runs side effects. Those are three separate structural questions, and none of them is answered by any single opcode.
Dense arms become a jump table
Start with a switch over adjacent enum members — a dense range:
public static string Short(DayOfWeek d) => d switch
{
DayOfWeek.Monday => "Mon",
DayOfWeek.Tuesday => "Tue",
DayOfWeek.Wednesday => "Wed",
_ => "?"
};
DayOfWeek.Monday is 1, Tuesday is 2, Wednesday is 3. Three adjacent values, so the compiler normalizes by subtracting the minimum and emits a single jump table:
.method public hidebysig static string Short(valuetype DayOfWeek d) cil managed
{
.locals init (string V_0)
ldarg.0
ldc.i4.1
sub // d - 1 -> 0,1,2 for Mon,Tue,Wed
switch (CASE_MON, CASE_TUE, CASE_WED)
br.s DEFAULT // offset outside [0,3) -> default
CASE_MON:
ldstr "Mon"
stloc.0
br.s END
CASE_TUE:
ldstr "Tue"
stloc.0
br.s END
CASE_WED:
ldstr "Wed"
stloc.0
br.s END
DEFAULT:
ldstr "?"
stloc.0
END:
ldloc.0
ret
}
Three things in this IL are the fingerprint of a switch expression. There is a single result local V_0. Every arm — including the default — stores into it and branches to one shared END label. And the whole thing resolves to ldloc.0; ret. That is a value being produced, not statements being run. The ldc.i4.1; sub before the switch is the normalization: to recover the real case labels, Glass adds the subtracted minimum back onto each table index, so target 0 is 1 (Monday), target 1 is 2 (Tuesday), and so on.
Sparse arms become a binary search
Now change the values so they are no longer adjacent:
public static string Status(int code) => code switch
{
200 => "OK",
404 => "NotFound",
500 => "ServerError",
_ => "Other"
};
A jump table normalized on 200 would need 301 slots to reach 500, almost all of them pointing at the default — wasteful. So the compiler abandons the table and emits a balanced binary search instead, comparing against the middle value first:
ldarg.0
stloc.0
ldloc.0
ldc.i4 404
bgt.s HIGH // code > 404 ? test the upper half
ldloc.0
ldc.i4 200
beq.s CASE_200
ldloc.0
ldc.i4 404
beq.s CASE_404
br.s DEFAULT
HIGH:
ldloc.0
ldc.i4 500
beq.s CASE_500
br.s DEFAULT
There is no switch opcode here at all — only bgt/beq branches arranged as a search tree, each leaf an equality test against one case constant. This is the same instruction vocabulary a hand-written if/else if chain produces. What still marks it as one switch is the shared structure underneath: every leaf tests the same hoisted value V_0, every match arm feeds the same result local and merge, and the non-matching path converges on one default. Glass collects the equality leaves of the search tree, reads each constant, and reassembles the arms in source order regardless of the order the binary search visits them.
switch jump table over a normalized offset; sparse arms become a balanced beq/bgt tree. Glass unifies both by the shared result temporary and merge label, undoes the offset to recover the real case constants, and reads a thrown SwitchExpressionException as proof the construct was an expression.
The arm that proves it was an expression
A switch statement and a switch expression can lower to byte-identical dispatch — the same jump table, the same binary search. What separates them is exhaustiveness. A switch expression must produce a value for every input, so when you omit a discard arm (_) and the arms are not provably exhaustive, the compiler synthesizes a default that throws:
DEFAULT:
ldloc.0
newobj instance void [System.Runtime]System.Runtime.CompilerServices.SwitchExpressionException::.ctor(object)
throw
That SwitchExpressionException has no source syntax — you never wrote a throw. It exists only because the expression form guarantees a result. When Glass sees a dispatch whose fall-through constructs and throws this exact type, and whose matching arms all converge on a single result temporary, it prints the expression form:
code switch { 200 => "OK", 404 => "NotFound", 500 => "ServerError" }
with no explicit default arm, because the throwing default is the implicit non-exhaustive tail. If instead the arms all store into a local and the default simply stores a value too — the "?"/"Other" cases above — Glass prints the _ => arm. The presence or absence of that synthesized throw is the difference between a four-arm switch with a discard and a three-arm exhaustive switch.
Strings take a different road
Integers have a natural order; strings do not, so neither a jump table nor a </> search tree applies directly. For a handful of string arms the compiler emits a straight chain of string.op_Equality calls. For a larger set it does something cleverer: it computes a hash of the input string, uses an integer switch on that hash to jump to a candidate bucket, and then confirms the match with a real string comparison before taking the arm. Glass recognizes this hash-then-confirm shape as a unit — the synthesized hash local and its switch are an implementation detail with no counterpart in your source — and recovers the original "literal" => arms from the confirming comparisons, discarding the hash machinery entirely.
Caveats and the honest limits
The reconstruction is faithful, but a few things do not survive the round trip. Arm order from a jump table is positional, not textual — the table records targets by value offset, so Glass recovers the arms sorted by case value, which may differ from the order you wrote them in when the result is identical either way. Switch expression versus a chain of ternaries can be genuinely ambiguous for two or three arms: x switch { 1 => a, 2 => b, _ => c } and x == 1 ? a : x == 2 ? b : c can lower to the same compares, and Glass prints the switch form only when the result-temp-and-merge shape matches exactly, falling back to the conditional otherwise. And when patterns, relational arms, and nested property patterns lower through a decision DAG rather than a flat jump table; those recover as the richer pattern syntax, which is a separate mechanism from the constant-arm dispatch shown here.
Takeaway
A switch expression leaves no single trace in IL. Dense constant arms compile to one switch opcode over a jump table normalized by subtracting the minimum case value; sparse arms compile to a balanced tree of beq/bgt comparisons indistinguishable instruction-for-instruction from a hand-written if chain; strings route through a synthesized hash. Glass recognizes all of these as one construct by the structure underneath — a single result temporary that every arm feeds, one shared merge label, and a recovered case constant behind each target — and it reads a thrown SwitchExpressionException as the proof that the construct yields a value rather than runs statements. Knowing that the jump table and the comparison tree are the same source construct under two compiler strategies is what lets you trust the switch Glass prints, whichever shape the IL happened to take.
Glass.NET is a free .NET decompiler with a VS-native Avalonia UI, a headless CLI, and an MCP server. It reads metadata and IL statically and never executes the assembly you open — point it at your own binaries or at third-party dependencies you are auditing or debugging. Grab it at /products/glass.
Top comments (0)