《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");
            }
Tagged with:     , , ,

About the author /


Hi,我是露露,喜歡登山,雖然很肉腳但是還是想挑戰百岳。 喜歡程式設計,雖然沒有非常的強,但是還是都可以完美的完成。

Post your comments

Your email address will not be published. Required fields are marked *