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.");
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值