IL switch instruction
//Why not "switch(IL_0026, IL_002c, IL_0032, IL_0038)"?
Because that 4th label would be jumped to if the value of a
was 4, not 15. The jump targets in a switch
correspond to successive values of the tested value.
In order to use switch
and not have the additional special case for 15
, it would need to be:
switch(IL_0026, IL_002c, IL_0032, IL_003e, IL_003e, IL_003e, IL_003e, IL_003e, IL_003e, IL_003e, IL_003e, IL_003e, IL_003e, IL_003e, IL_0038)
(I hope I've counted right)
Which would be a much larger jump table.
I'd always recommend checking out the documentation for Reflection.Emit
for the instruction you're looking at, e.g. OpCodes.Switch
, to make sure you understand what the instruction does.
The switch instruction pops a value off the stack and compares it, as an unsigned integer, to
N
. If value is less thanN
, execution is transferred to the target indexed by value, where targets are numbered from 0 (for example, a value of 0 takes the first target, a value of 1 takes the second target, and so on).