且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[导入]Why does my switch statement works differently?

更新时间:2022-09-08 10:56:00


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 something

case 1:

Console.WriteLine(x)

// do something in common with 0

default:

Console.WriteLine(x)

// do something in common with 0, 1 and everything else

break;

}



class Test

{

static void Main()

{

int x = 3;

 

switch (x)

{

case 0:

// do something

goto case 1;

case 1:

// do something in common with 0

goto default;

default:

// do something in common with 0, 1, and anything else

break;

}

}

}

本文转自斯克迪亚博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2004/09/22/45404.html,如需转载请自行联系原作者