C# 语言规范--1.6 语句(转)

C# 中的大多数语句都是直接从 C 和 C++ 借用的,但有一些值得注意的添加和修改。下表列出了可用的语句类型,并提供了每种类型的示例。

static void Main() { F(); G(); { H(); I(); } }
goto 语句static void Main(string[] args) { if (args.Length == 0) goto done; Console.WriteLine(args.Length);done: Console.WriteLine("Done");}
static void Main() { const float pi = 3.14f; const int r = 123; Console.WriteLine(pi * r * r);}
static void Main() { int a; int b = 2, c = 3; a = 1; Console.WriteLine(a + b + c);}
static int F(int a, int b) { return a + b;}
static void Main() {    F(1, 2);  // Expression statement}
if 语句static void Main(string[] args) { if (args.Length == 0) Console.WriteLine("No args"); else Console.WriteLine("Args");}
switch 语句static void Main(string[] args) { switch (args.Length) { case 0: Console.WriteLine("No args"); break; case 1: Console.WriteLine("One arg "); break; default: int n = args.Length; Console.WriteLine("{0} args", n); break; }}
while 语句static void Main(string[] args) { int i = 0; while (i < args.Length) { Console.WriteLine(args[i]); i++; }}
do 语句static void Main() { string s; do { s = Console.ReadLine(); } while (s != "Exit");}
for 语句static void Main(string[] args) { for (int i = 0; i < args.Length; i++) Console.WriteLine(args[i]);}
foreach 语句static void Main(string[] args) { foreach (string s in args) Console.WriteLine(s);}
break 语句static void Main(string[] args) { int i = 0; while (true) { if (i == args.Length) break; Console.WriteLine(args[i++]); }}
continue 语句static void Main(string[] args) { int i = 0; while (true) { Console.WriteLine(args[i++]); if (i < args.Length) continue; break; }}
return 语句static int F(int a, int b) { return a + b;}
static void Main() {    Console.WriteLine(F(1, 2));    return;}
throw 语句和 try 语句static int F(int a, int b) { if (b == 0) throw new Exception("Divide by zero"); return a / b;}
static void Main() {    try {        Console.WriteLine(F(5, 0));    }    catch(Exception e) {        Console.WriteLine("Error");    }}
checked 和 unchecked 语句static void Main() { int x = Int32.MaxValue; Console.WriteLine(x + 1); // Overflow checked { Console.WriteLine(x + 1); // Exception } unchecked { Console.WriteLine(x + 1); // Overflow }}
lock 语句static void Main() { A a = ...; lock(a) { a.P = a.P + 1; }}
using 语句static void Main() { using (Resource r = new Resource()) { r.F(); }}
[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-924501/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8781179/viewspace-924501/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值