《MCTS.C#》(part.4)第五章:流程控制、迴圈與例外狀況處理

  •  switch

在case中可以使用goto default; 或是goto case “…”;

以避免重覆的程式碼。

            switch (input)
            {
                case "111":
                    Console.WriteLine("...");
                    goto case "password"; //goto case "..." , 要記得要有case

                case "password":
                    Console.WriteLine("o");
                    break;
case "other":
                    Console.WriteLine("other...");
                    goto default; //goto label
                default:
                    Console.WriteLine("x");
                    break;
            }

 try – finally
無論如何都會執行finally,所以適合用於釋放資源。

 try
            {
                return; //即使return,仍會執行finally
                decimal result = i / j;
            }
            catch { }
            finally
            {
                Console.WriteLine("finally");
            }

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *