+ 7

Why do not remove at all the use of break in switch?

Since fall-through is disabled in c#, the use of break is mandatory in C# and not optional as in C++. So, according to me, its use seems redundant and could be avoided changing the semantics of switch statement. Maybe it is used only for compatibility with old compiler and other languages. Is it true or there is other reasons? I found something about this here: http://stackoverflow.com/questions/3108888/why-does-c-sharp-have-break-if-its-not-optional

18th May 2017, 6:08 AM
Daniele Gaito
Daniele Gaito - avatar
3 Respuestas
+ 1
there is an answer in the comments to that link. as i understand it, the authors: 1) removed fall through behaviour because it is a common cause of logic error at maintenance time. 2) make the break mandatory to avoid discrepency with other similar languages
20th May 2017, 3:11 PM
ifl
ifl - avatar
+ 1
if break was not mandatory the language would behave differently to other language even though the code is similar. this would be a huge source of errors when developers swap between languages.
20th May 2017, 3:14 PM
ifl
ifl - avatar
0
If you would remove these break statements the processor of your computer would not understand when to stop, hence a lot of errors would occur
28th Jun 2017, 8:06 AM
Wouter Michawitz
Wouter Michawitz - avatar