One thought on “Is there a advantage/disadvantage to using an if/else statement vs. a switch statement in c#? Does it affect the run time performance?

  1. The answer depends on whether your web development program is running in debug mode or release mode. In debug mode the switch statement produces the same assembly as If statements, however in release mode it will be compiled into jump table.
    In the .net programming language C# you can have switch statements on String constants. However it is not practical to build jump tables for strings of arbitrary lengths, so most often such switch will be compiled into stack of Ifs.
    Depending on the number of conditions, if they are large enough, C# compiler will create a Hash Table object, populate it with string constants and make a lookup on that table followed by jump. Therefore it will be significantly faster than comparing each string constant in Ifs, if the number of case labels is large

Comments are closed.