switch(C# 参考)

switch 语句是一个控制语句,选择“开关部分” 从候选列表中执行。

每个开关部分包含一个或多个 用例标签 和含有一个或多个语句的列表。 以下示例显示了一个包含三个切换字段的简单 switch 语句。 每个开关的部分有一个用例标签(例如 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;
}

每个用例标签指定一个常量值。 控件将传递给开关部分,其用例标签包含一个与开关表达式的值(即 caseSwitch)匹配的常量值。 如果 case 标签不包含匹配的值,则控件会传输给 default 节(如果有的话)。 如果没有 default 部分,则不会执行任何操作,并且会在 switch 语句之外传输控制。 在上一个示例中,执行第一开关节中的语句,因为 case 标签 case 1 指定值 1,caseSwitch 值也为 1。

switch 语句可包含任意数量的开关节,且每个节可具有一个或多个案例标签。 但是,任何两个案例的标签都不可以包含相同的常量值。

执行选定节的语句列表以第一个语句开始并且语句列表中继续进行,通常直到到达一个跳转语句,如 break,goto case,return,或throw。 此时,控制将传递到 switch 语句的外部或另一个事例标签。

与 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# 中的要求是每个开关部分的结尾,包括最后一个,都不可访问。 虽然通常使用跳转语句满足此要求,但以下情况下也有效,因为无法到达语句列表的末尾。

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;
        }
    }
}

在最后的示例中,输入字符串将转换为整数变量 switchExp,该变量用于 switch 表达式。 还可以直接使用字符串变量 str。 要做到这一点,您应改变案例的标签,以指定字符串的值,如下面的代码所示。

switch(str)
{
    case "1":
        // ...
    case "2":
        // ...
}
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 switchExp = int.Parse(str);
        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 (switchExp)
        {
            case 1:
                cost += 25;
                break;
            case 2:
                cost += 25;
                goto case 1;
            case 3:
                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# 语法和用法的权威资料。

请参见

参考

C# 关键字

switch语句(C++)

if-else(C# 参考)

概念

C# 编程指南

其他资源

C# 参考