我爱Aspx >> Asp.Net >> Beginner with c# 71。7 语句(Statements)
c#借用了c/c++大多数的语句方法,不过仍然有些值得注意的地方。还有些地方是有所改动的。
在这里,我只提一些c#特有的东东。
1。7。10 “foreach”语句
“foreach”语句列举一个集合内的所有元素,并对这些元素执行一系列的操作。还是看看例子吧:*/
using System;
using System.Collections;
class Test
{
static void WriteList(ArrayList list) {
foreach (object o in list)
{
int i = (int) o;//如果是for语句,这里一定会报错!
Console.WriteLine(0);
Console.WriteLine(++i);
}
}
static void Main() {
ArrayList list = new ArrayList();
for (int i = 0; i < 10; i++)
list.Add(i);
WriteList(list);
}
}
/*这个例子用“foreach”扫描了整个“list”,并把“list”中所有的元素打印出来。有时候还是
挺方便的。
1。7。15 安全检查开关(The checked and unchecked statements)
“checked”和“unchecked”语句用来控制数学运算和完整类型转换的检查工作。“checked”检查它
作用的域中可能出现的违例,并抛出一个异常;而“unchecked”则阻止所有的检查。举个例子:*/
using System;
class Test
{
static int x = 1000000;
static int y = 1000000;
static int F() {
checked {return (x * y);} // 抛出 OverflowException
}
static int G() {
unchecked {return (x * y);} // 返回 -727379968
}
static int H() {
return x * y; // 缺省状态。
}
static void Main() {
F(); //可以注销掉此行试试。
Console.WriteLine(G());
Console.WriteLine(H());
}
}
/*
在编译过程中不会有任何错误出现。因为“checked”和“unchecked”只在运行时才起作用。值得一说的是
Ҷƪл˵?
Beginner with c# 6[05-05]
Beginner with c# 5[05-05]
Beginner with c# 4[05-05]
Beginner with c# 3[05-05]
Beginner with c# 2[05-05]
Beginner with C#[05-05]
源码推荐:一个使用C#绘制图形引..[05-05]
在C#里如何调用标准DLL函数[05-05]
C#抢鲜快报之FAQ20[05-05]
Beginner with c# 7[05-05]
Beginner with c# 6[05-05]
Beginner with c# 5[05-05]
Beginner with c# 4[05-05]
Beginner with c# 3[05-05]
Beginner with c# 2[05-05]
Beginner with C#[05-05]
C++: BIG5到GB[05-05]
使用ASP和Word进行服务器端拼写检..[05-05]
处理内存泄漏的一种MFC方法[05-05]
Java会四分五裂吗?[05-05]