+ 3

If or switch?

Is there any situation in which It'd be better to choose if instead of switch?

14th Jan 2019, 3:43 PM
Grasshopper Hell Monkey
Grasshopper Hell Monkey - avatar
3 Answers
+ 4
Mina is right. For just a few items, the difference is small. But if you have many items you should definitely use a switch - switch have better performances. http://www.dotnetperls.com/if-switch-performance
14th Jan 2019, 5:33 PM
Arčibald Rajs
+ 5
If you have 1 or 2 options it's maybe better (and more common) to use if statement but it's your choice.
14th Jan 2019, 3:45 PM
Mina
Mina - avatar
+ 2
And I forget one important thing: 99,999% of the time you shouldn't care about performances if vs. switch. :)
14th Jan 2019, 5:35 PM
Arčibald Rajs