switch

 

switch 语句是一个控制语句,用于从候选列表中选择一个要执行的开关部分。

switch 语句包含一个或多个开关部分。 每个开关部分包含一个或多个 case 标签,后接一个或多个语句。 下面的示例展示了一个包含三个开关部分的简单 switch 语句。 每个开关部分各有一个 case 标签(例如 case 1)和两个语句。

int caseSwitch = 1;
switch (caseSwitch)
{
    case 1:
        Console.WriteLine("Case 1");
        break;
    case 2:
        Console.WriteLine("Case 2");
        break;
    default:
        Console.WriteLine("Default case");
        break;
}

备注

每个 case 标签指定一个常数值。 switch 语句会将控制传输到 case 标签与 switch 表达式的值(示例中为 caseSwitch)相符的开关部分。 如果任何 case 标签都不包含匹配值,则将控制传输到 default 部分(如果有)。 如果没有 default 部分,则不会执行任何操作,并在 switch 语句之外传输控制。 在上一个示例中,因为 case 1caseSwitch 的值匹配,因此执行第一个开关部分中的语句。

switch 语句中可以包含任意数量的开关部分,每个开关部分可以具有一个或多个 case 标签(如下面的字符串 case 标签示例中所示)。 但是,任何两个 case 标签不可包含相同的常数值。

执行选定开关部分中的语句列表时,将首先执行第一个语句,然后执行整个语句列表,通常直到到达一个跳转语句为止,如 breakgoto casereturnthrow 此时,控件在 switch 语句之外进行传输或传输到另一个 case 标签。

与 C++ 不同、C# 不允许从一个开关部分继续执行到下一个开关部分。 下面的代码会导致错误。

switch (caseSwitch)
{
    // The following switch section causes an error.
    case 1:
        Console.WriteLine("Case 1...");
        // Add a break or other jump statement here.
    case 2:
        Console.WriteLine("... and/or Case 2");
        break;
}

C# 要求开关部分(包括最后一个)的末尾不可到达。就是说,不同于其他一些语言,代码不能落入下一个开关部分。虽然此要求通常使用 break 语句来满足,但以下情况同样有效,因为它可以确保无法到达语句列表的末尾。

case 4:
    while (true)
        Console.WriteLine("Endless looping. . . .");

示例

下面的示例演示 switch 语句的要求和功能。

class Program
{
    static void Main(string[] args)
    {
        int switchExpression = 3;
        switch (switchExpression)
        {
            // A switch section can have more than one case label.
            case 0:
            case 1:
                Console.WriteLine("Case 0 or 1");
                // Most switch sections contain a jump statement, such as
                // a break, goto, or return. The end of the statement list
                // must be unreachable.
                break;
            case 2:
                Console.WriteLine("Case 2");
                break;
                // The following line causes a warning.
                Console.WriteLine("Unreachable code");
            // 7 - 4 in the following line evaluates to 3.
            case 7 - 4:
                Console.WriteLine("Case 3");
                break;
            // If the value of switchExpression is not 0, 1, 2, or 3, the
            // default case is executed.
            default:
                Console.WriteLine("Default case (optional)");
                // You cannot "fall through" any switch section, including
                // the last one.
                break;
        }
    }
}

示例

在最后一个示例中,字符串变量、str 和字符串 case 标签控制执行流。

class SwitchTest
{
    static void Main()
    {
        Console.WriteLine("Coffee sizes: 1=small 2=medium 3=large");
        Console.Write("Please enter your selection: ");
        string str = Console.ReadLine();
        int cost = 0;

        // Notice the goto statements in cases 2 and 3. The base cost of 25
        // cents is added to the additional cost for the medium and large sizes.
        switch (str)
        {
            case "1":
            case "small":
                cost += 25;
                break;
            case "2":
            case "medium":
                cost += 25;
                goto case "1";
            case "3":
            case "large":
                cost += 50;
                goto case "1";
            default:
                Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
                break;
        }
        if (cost != 0)
        {
            Console.WriteLine("Please insert {0} cents.", cost);
        }
        Console.WriteLine("Thank you for your business.");
    }
}
/*
    Sample Input: 2

    Sample Output:
    Coffee sizes: 1=small 2=medium 3=large
    Please enter your selection: 2
    Please insert 50 cents.
    Thank you for your business.
*/

C# 语言规范

有关更多信息,请参见C# 语言规范。 该语言规范是 C# 语法和用法的权威资料。