Groups | Blog | Home
all groups > dotnet performance > june 2007 >

dotnet performance : switch statement vs if-else-if



Prabhakar Ahinave
6/4/2007 12:00:00 AM
Can anyone tell when to use switch statement and when to if-else-if
construct?



Jon Skeet [C# MVP]
6/4/2007 12:00:00 AM
[quoted text, click to view]

Do you only care about the performance (given the group you've posted
in) or code style in general?

For performance, I suspect it depends on various things - I wouldn't
like to guess without trying.

For coding style, I tend to prefer to have a switch block if there are
more than two branches - and if the thing naturally lends itself to a
switch to start with, of course. I don't artificially introduce values
which are appropriate for a switch just for the sake of using one.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
AddThis Social Bookmark Button