C#关键字 之 块

选择语句

if(x==1){
//语句1
} else{
//语句2
}
switch(x){
case 1:  
//语句1
break;//必写
case 2:
//语句2
break;
default://其它
//语句3
break;
}

迭代语句

for(int i=0;i<100;i++){
//...
}
foreach(int i in x){
//...use i
}
int x = 0;
do{
x++;//先执行后判断
} while (x < 5);
	int n = 1;
        while (n < 6) 
        {
            n++;//先执行后判断
        }

 

break跳出循环

continue跳出循环的本次执行

goto跳到任意行编号

return函数返回值

yield返回一个值到一个集合

public static IEnumerable Power(int number, int exponent){
        int counter = 0;
        int result = 1;
        while (counter++ < exponent)
        {
            result = result * number;
            yield return result;
        }
    }

    static void Main()
    {
        foreach (int i in Power(2, 8))
        {
            Console.Write("{0} ", i);
        }
    }

 

throw 抛出异常

try-catch获取异常

try-finally解决异常

try-catch-finally上两个结合

 

checked检查溢出(溢出就出错)

unchecked不查(溢出自动截了)

posted @ 2008-10-06 20:43  tiny羊  阅读(318)  评论(0)    收藏  举报