Stricter and Sharper CSharp with Switch Statements :: A Cool Feature for Other Language Compilers to Emulate
The most cute and lovely feature of C#, in contrast with other languages is the mandate that it places on 'break' statement in switch constructs. This saves great time and effort in inadvertant overlook of break statement and control executing all the case blocks. Now, if there is no break construct between two case blocks, compiler would shout an error like 'Control can not fall through between constructs'. One exception being, you can have blank case statements.
case 1:
case 2:
case 3:
can be deemed valid. However, if there is even one line of executable statement in any case, it has to be either followed by a break. If you really want the next case to be executed, you can try using a goto statement.
Just thought I would share this feature with other readers and I hope other languages also would try emulating this in their compilers.
The most cute and lovely feature of C#, in contrast with other languages is the mandate that it places on 'break' statement in switch constructs. This saves great time and effort in inadvertant overlook of break statement and control executing all the case blocks. Now, if there is no break construct between two case blocks, compiler would shout an error like 'Control can not fall through between constructs'. One exception being, you can have blank case statements.
case 1:
case 2:
case 3:
can be deemed valid. However, if there is even one line of executable statement in any case, it has to be either followed by a break. If you really want the next case to be executed, you can try using a goto statement.
Just thought I would share this feature with other readers and I hope other languages also would try emulating this in their compilers.
No comments:
Post a Comment