C# 中的大多数语句都是直接从 C 和 C++ 借用的,但有一些值得注意的添加和修改。下表列出了可用的语句类型,并提供了每种类型的示例。 语句 示例 语句列表和块语句 static void Main() { 标记语句和 goto 语句 static void Main(string[] args) { done: 局部常数声明 static void Main() { 局部变量声明 static void Main() { 表达式语句 static int F(int a, int b) { if 语句 static void Main(string[] args) { switch 语句 static void Main(string[] args) { while 语句 static void Main(string[] args) { do 语句 static void Main() { for 语句 static void Main(string[] args) { foreach 语句 static void Main(string[] args) { break 语句 static void Main(string[] args) { continue 语句 static void Main(string[] args) { return 语句 static int F(int a, int b) { throw 语句和 try 语句 static int F(int a, int b) { checked 和 unchecked 语句 static void Main() { Console.WriteLine(x + 1); // Overflow checked { unchecked { lock 语句 static void Main() { using 语句 static void Main() {
F();
G();
{
H();
I();
}
}
if (args.Length == 0)
goto done;
Console.WriteLine(args.Length);
Console.WriteLine("Done");
}
const float pi = 3.14f;
const int r = 123;
Console.WriteLine(pi * r * r);
}
int a;
int b = 2, c = 3;
a = 1;
Console.WriteLine(a + b + c);
}
return a + b;
}
static void Main() {
F(1, 2); // Expression statement
}
if (args.Length == 0)
Console.WriteLine("No args");
else
Console.WriteLine("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;
}
}
int i = 0;
while (i < args.Length) {
Console.WriteLine(args[i]);
i++;
}
}
string s;
do { s = Console.ReadLine(); }
while (s != "Exit");
}
for (int i = 0; i < args.Length; i++)
Console.WriteLine(args[i]);
}
foreach (string s in args)
Console.WriteLine(s);
}
int i = 0;
while (true) {
if (i == args.Length)
break;
Console.WriteLine(args[i++]);
}
}
int i = 0;
while (true) {
Console.WriteLine(args[i++]);
if (i < args.Length)
continue;
break;
}
}
return a + b;
}
static void Main() {
Console.WriteLine(F(1, 2));
return;
}
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");
}
}
int x = Int32.MaxValue;
Console.WriteLine(x + 1); // Exception
}
Console.WriteLine(x + 1); // Overflow
}
}
A a = ...;
lock(a) {
a.P = a.P + 1;
}
}
using (Resource r = new Resource()) {
r.F();
}
}