VisualC#编程入门之语句 |
|
www.nanhushi.com 佚名 不详 |
程序的活动是通过语句(statement)来表达的。C#支持几种不同的语句,许多语句是以嵌入语句的形式定义的。
块(block)允许在只能使用单个语句的上下文中编写多个语句。块由一个括在大括号“{}”内的语句列表组成。 声明语句(declaration statement)用于声明局部变量和常量。
表达式语句(expression statement)用于运算表达式。表达式可以作为语句使用译注3,包括方法调用、使用new运算符进行对象分配、使用“=”和复合赋值运算符进行赋值,以及使用“++”和“--”运算符进行增量和减量的运算。 选择语句(selection statement)用于根据某个表达式的值,选择执行若干可能语句中的某一个。这一组语句有if和switch语句。
迭代语句(iteration statement)用于重复执行嵌入语句。这一组语句有while,do,for和foreach语句。
跳转语句(jump statement)用于传递程序控制。这一组语句有break,continue,goto,throw和return语句。
try-catch语句用于捕捉在块的执行期间发生的异常。并且,try-finally语句用于指定一个终止代码块,不管异常出现与否,它总是被执行。
checked和unchecked语句用于控制整型算术运算和转换的溢出检查上、下文。
lock语句用于获取给定对象的互斥锁,执行语句,然后释放该锁。
using语句用于获取一个资源,执行一个语句,然后处理该资源。
表1.5列出了C#的语句,并逐个提供了示例。
表1.5 C#的语句
语 句 |
示 例
|
局部变量声明
|
static void Main(){
int a;
int b=2,c=3;
a=1;
Console.WriteLine(a+b+c);
}
|
局部常量声明
|
static void Main(){
const float pi=3.1415927f;
const int r=25;
Console.WriteLine(pi * r * r);
}
|
表达式语句
|
static void Main(){
int i;
i=123; //表达式语句
Console.WriteLine(i); //表达式语句
i++; //表达式语句
Console.WriteLine(i); //表达式语句
}
|
if语句
|
static void Main(string[] args){
if(args.Length == 0){
Console.WriteLine("No arguments");
}
else{
Console.WriteLine("One or more arguments");
}
}
|
(续表)
语 句
|
示 例
|
switch语句
|
static void Main(string[] args){
int n = args.Length;
switch(n){
case 0:
Console.WriteLine("No arguments");
break;
case 1:
Console.WriteLine("One argument");
break;
default:
Console.WriteLine("{0} arguments", 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();
if(s!=null) Console.WriteLine(s);
} while(s != null);
}
|
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(){
while(true){
string s = Console.ReadLine();
if (s == null) break;
Console.WriteLine(s);
}
}
|
continue语句
|
static void Main(string[] args){
for(int i = 0; i < args.Length; i++){
if (args[i].StartsWith("/")) continue;
Console.WriteLine(args[i]);
}
}
|
goto语句
|
static void Main(string[] args){
int i = 0;
goto check;
loop:
Console.WriteLine(args[i++]);
check:
if (i < args.Length) goto loop;
}
|
(续表)
语 句
|
示 例
|
return语句
|
static int Add(int a, int b){
return a + b;
}
static void Main(){
Console.WriteLine(Add(1, 2));
return;
}
|
throw和try语句
|
static double Divide(double x, double y) {
if (y == 0) throw new DivideByZeroException();
return x / y;
}
static void Main(string[] args){
try{
if (args.Length !=2){
throw new Exception("Two numbers required");
}
double x = double.Parse(args[0]);
double y = double.Parse(args[1]);
Console.WriteLine(Divide(x, y));
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
|
checked和unchecked语句
|
static void Main(){
int i = int.MaxValue;
checked {
Console.WriteLine(i + 1); //异常
}
unchecked {
Console.WriteLine(i + 1); //溢出
}
}
|
lock语句
|
class Account
{
decimal balance;
public void Withdraw(decimal amount) {
lock(this) {
if (amount > balance) {
throw new Exception("Insufficient funds");
}
balance -= amount;
}
}
}
|
using语句
|
static void Main(){
using (TextWriter w = File.CreateText("test.txt")) {
w.WriteLine("Line one");
w.WriteLine("Line two");
w.WriteLine("Line three");
}
}
|
|
|
|
文章录入:杜斌 责任编辑:杜斌 |
|
上一篇文章: C#3.0语言详解之基本的语言增强 下一篇文章: VisualC#编程入门之表达式 |
【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
|