switch (C# 參考)

switch 陳述式是控制項陳述式,該陳述式會從候選項清單中選取要執行的「參數區段」(Switch Section)。

switch 陳述式包含一個或多個參數區段。 每個參數區段都包含一個或多個「case 標籤」(Case Label),後面接著一個或多個陳述式。 下列範例將示範擁有三個參數區段的簡單 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 運算式」(Switch Expression) 之值的參數區段 (範例中為 caseSwitch)。 如果沒有任何 case 標籤包含相符的值,則控制項會傳輸至 default 區段 (如果有的話)。 如果沒有 default 區段,則不會採取任何動作,而且控制項會在 switch 陳述式之外傳輸。 在上面的範例中,因為 case 1 符合 caseSwitch 的值,所以第一個參數區段中的陳述式會執行。

switch 陳述式可包含任意數目的參數區段,而每個區段都可以擁有一個或多個 case 標籤 (如下面的字串 case 標籤範例中所示)。 但是,不可以有兩個 case 標籤包含相同的常數值。

在選取的參數區段中,陳述式清單是從第一個陳述式開始執行,然後繼續進行整份陳述式清單,通常會進行直到跳躍陳述式為止,例如到達 break、goto case、return 或 throw。 到達該點時,控制項會在 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 仍有效,因為它可確保無法到達陳述式清單的結尾。

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# 語法及用法的限定來源。

請參閱

參考

C# 關鍵字

switch 陳述式 (C++)

if-else (C# 參考)

概念

C# 程式設計手冊

其他資源

C# 參考