C#語言基礎(二)

  
流程控制
8、if….else語句
using System;
using System.Collections.Generic;
using System.Text;
//if...else選擇語句
namespace IfElseTest
{
    class IfElseTest
    {
        static void Main(string[] args)
        {
            bool flagCheck = true;
            if (flagCheck == true)
            {
                Console.WriteLine("The flag is set to true.");
            }
            else
            {
                Console.WriteLine("The flag is set to false.");
            }
        }
    }
}
9、switch語句
using System;
using System.Collections.Generic;
using System.Text;
//switch語句
namespace SwitchTest
{
    class SwitchTest
    {
        static void Main(string[] args)
        {
            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;
            }
        }
    }
}
10、for循環語句
using System;
using System.Collections.Generic;
using System.Text;
//for循環語句
namespace ForLoopTest
{
    class ForLoopTest
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 5; i++)
            {
                Console.WriteLine(i);
            }
        }
    }
}
11、while循環語句
using System;
using System.Collections.Generic;
using System.Text;
//while循環語句
namespace WhileText
{
    class WhileTest
    {
        static void Main(string[] args)
        {
            int n = 1;
            while (n < 6)
            {
                Console.WriteLine("Current value of n is {0}", n);
                n++;
            }
        }
    }
}
12、do…while循環語句
using System;
using System.Collections.Generic;
using System.Text;
//do...while循環語句
namespace TestDoWhile
{
    class TestDoWhile
    {
        static void Main(string[] args)
        {
            int x = 0;
            do
            {
                Console.WriteLine(x);
                x++;
            }
            while (x < 5);
        }
    }
}
13、foreach語句
using System;
using System.Collections.Generic;
using System.Text;
//foreach語句
namespace ForeachTest
{
    class ForeachTest
    {
        static void Main(string[] args)
        {
            int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
            foreach (int i in fibarray)
            {
                Console.WriteLine(i);
            }
        }
    }
}
14、throw語句
using System;
using System.Collections.Generic;
using System.Text;
//throw語句
namespace ThrowTest
{
    class ThrowTest
    {
        static void Main(string[] args)
        {
            string s = null;
            if (s == null)
            {
                throw new ArgumentNullException();
            }
            Console.Write("The string is null.");
        }
    }
}
15、try…catch語句
using System;
using System.Collections.Generic;
using System.Text;
//try...catch語句,try塊包含對可能導致異常的MyMethod()方法的調用。
//catch子句包含僅在屏幕上顯示消息的異常處理程序。
//當從MyMethod()內部調用throw語句時,系統查找catch語句並顯示Exception caught消息。
namespace TrycatchTest
{
    class TrycatchTest
    {
        static void Main(string[] args)
        {
            try
            {
                string s = null;
                ProcessString(s);
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }
        }
        static void ProcessString(string s)
        {
            if (s == null)
            {
                throw new ArgumentNullException();
            }
        }
    }
}
16、try…finally語句
using System;
using System.Collections.Generic;
using System.Text;
//try...finally語句,代碼中有一個導致異常的無效轉換語句,當程序運行時,將產生一條運行時錯誤信息,
//但程序沒有去處理異常信息,而是控制傳遞給finally子句繼續執行並顯示輸出。
namespace TryFinallyTest
{
    class TryFinallyTest
    {
        static void Main(string[] args)
        {
            int i = 123;
            string s = "Some string";
            object o = s;
            try
            {
                i = (int)o;//包含一個字符串,無法轉換爲int類型
            }
            finally
            {
                Console.Write("i={0}", i);
            }
        }
    }
}
17、try…catch…finally語句
using System;
using System.Collections.Generic;
using System.Text;
//try...catch...finally語句,代碼演示了捕捉異常,並作出相應處理。
namespace TryCatchFinallyTest
{
    class TryCatchFinallyTest
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Executing the try statement.");
                throw new NullReferenceException();
            }
            catch (NullReferenceException e)
            {
                Console.WriteLine("{0} Caught exception #1.", e);
            }
            catch
            {
                Console.WriteLine("Caught exception #2.");
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章