编译器错误 CS0163

控制不能从一个 case 标签(“label”)贯穿到另一个 case 标签

switch 语句 包含多个时开关部分,使用以下关键字之一,必须显式终止每节,其中包括最后一个:

如果要由一部分到下一部分实现“贯穿”的行为,使用 goto case #。有关更多信息和示例,请参见switch(C# 参考)

下面的示例生成 CS0163。

// CS0163.cs
public class MyClass
{
    public static void Main()
    {
        int i = 0;

        switch (i)   // CS0163
        {
            // Compiler error CS0163 is reported on the following line.
            case 1:
                i++;
            // To resolve the error, uncomment one of the following example statements.
            // return;
            // break;
            // goto case 3;

            case 2:
                i++;
                return;

            case 3:
                i = 0;
                return;

            // Compiler error CS0163 is reported on the following line.
            default:
                Console.WriteLine("Default");
                // To resolve the error, uncomment the following line:
            //break;
        }
    }