C#指南,重温基础,展望远方!(5)C#语句
2018-05-30 09:35
190 查看
程序操作使用语句进行表示。 C# 支持几种不同的语句,其中许多语句是从嵌入语句的角度来定义的。
使用代码块,可以在允许编写一个语句的上下文中编写多个语句。 代码块是由一系列在分隔符
{和
}内编写的语句组成。
声明语句用于声明局部变量和常量。
1.表达式语句用于计算表达式。 可用作语句的表达式包括方法调用、使用
new运算符的对象分配、使用
=和复合赋值运算符的赋值、使用
++和
--运算符和
await表达式的递增和递减运算。
2.选择语句用于根据一些表达式的值从多个可能的语句中选择一个以供执行。 这一类语句包括
if和
switch语句。
3.迭代语句用于重复执行嵌入语句。 这一类语句包括
while、
do、
for和
foreach语句。
4.跳转语句用于转移控制权。 这一类语句包括
break、
continue、
goto、
throw、
return和
yield语句。
5.try...
catch语句用于捕获在代码块执行期间发生的异常,
try...
finally语句用于指定始终执行的最终代码,无论异常发生与否。
6.checked和
unchecked语句用于控制整型类型算术运算和转换的溢出检查上下文。
7.lock语句用于获取给定对象的相互排斥锁定,执行语句,然后解除锁定。
8.using语句用于获取资源,执行语句,然后释放资源。
下面列出了可以使用的各种语句,以及每种语句的示例。
- 局部变量声明:
static void Declarations(string[] args) { int a; int b = 2, c = 3; a = 1; Console.WriteLine(a + b + c); }
- 局部常量声明:
static void ConstantDeclarations(string[] args) { const float pi = 3.1415927f; const int r = 25; Console.WriteLine(pi * r * r); }
- 表达式语句:
static void Expressions(string[] args) { int i; i = 123; // Expression statement Console.WriteLine(i); // Expression statement i++; // Expression statement Console.WriteLine(i); // Expression statement }
if
语句:
static void IfStatement(string[] args) { if (args.Length == 0) { Console.WriteLine("No arguments"); } else { Console.WriteLine("One or more arguments"); } }
switch
语句:
static void SwitchStatement(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($"{n} arguments"); break; } }
while
语句:
static void WhileStatement(string[] args) { int i = 0; while (i < args.Length) { Console.WriteLine(args[i]); i++; } }
do
语句:
static void DoStatement(string[] args) { string s; do { s = Console.ReadLine(); Console.WriteLine(s); } while (!string.IsNullOrEmpty(s)); }
for
语句:
static void ForStatement(string[] args) { for (int i = 0; i < args.Length; i++) { Console.WriteLine(args[i]); } }
foreach
语句:
static void ForEachStatement(string[] args) { foreach (string s in args) { Console.WriteLine(s); } }
break
语句:
static void BreakStatement(string[] args) { while (true) { string s = Console.ReadLine(); if (string.IsNullOrEmpty(s)) break; Console.WriteLine(s); } }
continue
语句:
static void ContinueStatement(string[] args) { for (int i = 0; i < args.Length; i++) { if (args[i].StartsWith("/")) continue; Console.WriteLine(args[i]); } }
goto
语句:
static void GoToStatement(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 ReturnStatement(string[] args) { Console.WriteLine(Add(1, 2)); return; }
yield
语句:
static IEnumerable<int> Range(int from, int to) { for (int i = from; i < to; i++) { yield return i; } yield break; } static void YieldStatement(string[] args) { foreach (int i in Range(-10,10)) { Console.WriteLine(i); } }
throw
和try
语句:
static double Divide(double x, double y) { if (y == 0) throw new DivideByZeroException(); return x / y; } static void TryCatch(string[] args) { try { if (args.Length != 2) { throw new InvalidOperationException("Two numbers required"); } double x = double.Parse(args[0]); double y = double.Parse(args[1]); Console.WriteLine(Divide(x, y)); } catch (InvalidOperationException e) { Console.WriteLine(e.Message); } finally { Console.WriteLine("Good bye!"); } }
checked
和unchecked
语句:
static void CheckedUnchecked(string[] args) { int x = int.MaxValue; unchecked { Console.WriteLine(x + 1); // Overflow } checked { Console.WriteLine(x + 1); // Exception } }
lock
语句:
class Account { decimal balance; private readonly object sync = new object(); public void Withdraw(decimal amount) { lock (sync) { if (amount > balance) { throw new Exception( "Insufficient funds"); } balance -= amount; } } }
using
语句:
static void UsingStatement(string[] args) { using (TextWriter w = File.CreateText("test.txt")) { w.WriteLine("Line one"); w.WriteLine("Line two"); w.WriteLine("Line three"); } }
相关文章推荐
- C#指南,重温基础,展望远方!(3)类型和变量
- C#指南,重温基础,展望远方!(4)表达式
- C#指南,重温基础,展望远方!(13)C#泛型
- C#指南,重温基础,展望远方!(6)C#类和对象
- C#指南,重温基础,展望远方!(7)C#结构
- C#指南,重温基础,展望远方!(8)C#数组
- C#指南,重温基础,展望远方!(9)C#接口
- C#指南,重温基础,展望远方!(10)C#枚举
- C#指南,重温基础,展望远方!(11)C#委托
- C#指南,重温基础,展望远方!(12)C#特性
- C#指南,重温基础,展望远方!(1)C#语言介绍
- C#指南,重温基础,展望远方!(2)程序结构
- 数据查询基础和C#连接数据库语句
- C#语言基础语句
- C#基础回顾以及if语句
- C#基础—— check、lock、using语句归纳
- C#基础的流程语句(九九乘法表)(百钱白鸡)
- C# 语言基础(简单的输出 输入语句 和 加法运算符)
- C#基础之While语句
- 5、C#基础整理(for 语句经典习题--与 if 的嵌套)