8.6 Statements

博客介绍了C#大多语句直接借鉴自C和C++,还有一些新增和修改。列举了多种语句类型,如语句列表和块语句、标签语句和goto语句等,并给出了每种语句的示例代码,涉及局部常量声明、局部变量声明等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8.6 Statements
C# borrows most of its statements directly from C and C++, though there are
some noteworthy additions and
modifications. The table below lists the kinds of statements that can be
used, and provides an example for
each.
Statement Example
Statement lists and block
statements
static void Main() {
F();
G();
{
H();
I();
}
}
Labeled statements and goto
statements
static void Main(string[] args) {
if (args.Length == 0)
goto done;
Console.WriteLine(args.Length);
done:
Console.WriteLine("Done");
}
Local constant declarations static void Main() {
const float pi = 3.14f;
const int r = 123;
Console.WriteLine(pi * r * r);
}
Local variable declarations static void Main() {
int a;
int b = 2, c = 3;
a = 1;
Console.WriteLine(a + b + c);
}
Expression statements static int F(int a, int b) {
return a + b;
}
static void Main() {
F(1, 2); // Expression statement
}
if statements static void Main(string[] args) {
if (args.Length == 0)
Console.WriteLine("No args");
else
Console.WriteLine("Args");
}
switch statements 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 statements static void Main(string[] args) {
int i = 0;
while (i < args.Length) {
Console.WriteLine(args[i]);
i++;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值