Why does my switch statement works differently?
C# does not support an explicit fall through for case blocks (unless the block is empty)
For an explanation of why, see Why is the C# switch statement designed to not allow fall through, but still require a break? on MSDN
The following code is not legal and will not compile in C#:
switch (x){case 0:Console.WriteLine(x)// do somethingcase 1:Console.WriteLine(x)// do something in common with 0default:Console.WriteLine(x)// do something in common with 0, 1 and everything elsebreak;}
In order to achieve the same effect in C# the code must be modified as shown below (notice how the control flows are very explicit):
class Test{static void Main(){int x = 3; switch (x){case 0:// do somethinggoto case 1;case 1:// do something in common with 0goto default;default:// do something in common with 0, 1, and anything elsebreak;}}}