for(int x = 0;x < 10; x ++) { if (x < 3) continue; Console.Write("{0}, ", x); }
// output 3, 4, 5, 6, 7, 8, 9,
return
结合方法使用
goto
很C语言, 高级语言见不到这样的语法。
和标签语句结合使用
无条件转移控制到一个标签语句
1 2 3 4 5 6 7 8
for(int x = 0;x < 10; x ++) { if (x == 3) goto aaa; Console.Write("{0}, ", x); } aaa: Console.WriteLine("tag aaa");
// output 0, 1, 2, tag aaa
throw
抛错
标签语句
标签语句由一个标识符后面跟着一个冒号再跟着一条语句组成。它有下面的形式。
Identifier: Statement
标签语句的执行完全如同标签不存在一样,并仅执行Statement部分。
给语句增加一个标签允许控制从代码的其他部分转移到该语句。
标签语句只允许用在块内部。
标签
不能是关键字
不能和另一个标签名称相同
标签语句作用域
external
using 语句
通常作为资源引入的 using 指令。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
using System; using System.IO; classProgram { staticvoidMain() { Console.WriteLine("hello wrold"); using (TextWriter tw = File.CreateText("Lincoln.txt")) { tw.WriteLine(" Four score and seven years ago, ..."); // 调用IO 生成文件并写入一短话。 } using (TextReader td = File.OpenText("Lincoln.txt")) { Console.WriteLine(td.ReadLine()); // Four score and seven years ago, ... } } }