Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop C#'s switch statement from generating the CIL switch instruction

C#'s switch statement can compile to a CIL switch instruction, or if/else's, depending on the cases in the statement as mentioned here. Is there a way to force the compiler to always generate the if/else variant in a block of code?

like image 882
user55017 Avatar asked Jan 23 '23 16:01

user55017


2 Answers

The simplest way would be to use if/else in your code. Apart from anything else, that makes it clearer to the reader that that's what you want to happen instead of using a switch.

EDIT: Okay, so the readability isn't important for you - but basically if you want the compiled code to change, the source code is going to have to change. You could use the Mono compiler and modify it yourself, but I doubt that there's any way of getting the Microsoft compiler to effectively ignore that you're using a switch statement.

like image 136
Jon Skeet Avatar answered Feb 23 '23 04:02

Jon Skeet


Have you tried a different compiler (i.e., Mono), or tried to place your offending classes in a separate assembly and switch to a different language for it?

like image 29
ballarewt Avatar answered Feb 23 '23 04:02

ballarewt