Fall-thru or not to fall through

Fall-thru or not to fall through

There is a little differences how C# and Java implement the switch-statement. I discovered it when I want to implement a fall through in C#.
In Java it is very simple: just leave the break statement at the end of the case block away.
In C# it doesn’t work like that. There is no implicit fall through (with one exception: The case blocks have to be empty). You have to implement it explicit by add at the end of the case block a goto statement which refer to the case block which should be called.
The advantage of the explicit way is, that a classic mistake couldn’t happened but the disadvantage is that you have to write more code and use the “old” keyword goto (and I thought I wouldn’t see this keyword again…). I found also an explanation for that behaviour on msdn.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.