表达式定义

什么是表达式

Expressions, together with commands and declarations, are one of the basic components of every rogramming language.We can say that expressions are the essential component of every language.
An expressions is a syntactic entity (语法实体)whose evaluation either produces a value or fails to terminate, in which case the expression is undefined.
各种编程语言对表达式的实现不尽相同,但大体上都符合这个定义

C#语言对表达式的定义

An expression is a sequence of one or more operands and zero or more operators that can be evaluated to a single value, object, method, or namespace.

  1. int x;
  2. x = 100;
  3. // single value
  4. (new Form()).showDialog();
  5. //object
  6. Action myAction = new Action(Console.WriteLine);
  7. //method
  8. System.Windows.Forms.Form myForm = new Forms();
  9. //namespace

Expressions can consist of a literal value, a method invocation, an operator and its operands, or a simple name.Simple names can be the name of a variable type member, method parameter,namespace or type.
算法逻辑的最基本(最小)单元,表达一定的算法意图
因为操作符有优先级,所以表达式也就有了优先级

表达式概览

C#语言中表达式的分类

  1. - A value.Every value has an associated type(任何值都有一个关联类型).任何能得到值的运算(回顾操作符和结果类型)

操作数的类型不代表最终结果的type是一致的。

  1. - A variable.(变量)Every variable has an associated type.
  2. - A namespace.
  3. - A type.
  4. - A method group(方法组).例如:Console.WriteLine 这是一组方法, 重载决策决定具体调用哪一个
  5. - A null literal(空值).
  6. - An anonymous function(匿名函数).
  7. - A property access.
  8. - An event access.
  9. - An indexer access.
  10. - Nothing.对返回值为void的方法的调用

表达式返回值概述

x.y操作符
返回值的类型由所访问的成员类型决定返回值的类型

  1. static void Main(string[] args)
  2. {
  3. Student stu = new Student();
  4. var x = stu.name; //x的type为string
  5. var y = stu.age; //y的type为int32
  6. }
  7. class Student
  8. {
  9. public string name;
  10. public int age;
  11. }

f(x)操作符
返回值的类型由f(x)中的f决定。

  1. static void Main(string[] args)
  2. {
  3. var x = Math.Pow(2, 3);//pow的类型为double,所以,x的类型为double
  4. Console.WriteLine(x.GetType ().Name );
  5. }

a[x]操作符
由集合类型所决定

  1. static void Main(string[] args)
  2. {
  3. List<int> intlist = new List<int>() { 1,2,3};
  4. double[] doubleList = new double[] { 1.0, 2.0, 3.0 };
  5. var x = intlist[1]; //x为int
  6. var y = doubleList[1]; //y为double
  7. Console.WriteLine(x.GetType());
  8. Console.WriteLine(y.GetType());
  9. }

x++ x-- ++x --x操作符
表达式的类型和x的类型一致。
表达式的值需要与 操作数的值区分开
typeof操作符
返回值固定,为type。
default操作符
所操作的数据值类型

  1. static void Main(string[] args)
  2. {
  3. var x =default (int);
  4. Console.WriteLine(x);
  5. Console.WriteLine(x.GetType().FullName );
  6. }

checked unchecked操作符
表达式与操作数的类型一致

  1. static void Main(string[] args)
  2. {
  3. var x = checked(123 + 456);
  4. Console.WriteLine(x.GetType().FullName );
  5. }

操作符
为布尔类型
~操作符
表达式与操作数的类型一致
(T)x操作符
表达式类型与需要转换类型一致
* / + - %算数运算操作符
在不发生“数值提升”的前提下,表达式类型与需要转换类型一致;
发生之后,与精度最高的操作数保持一致;
<< >>操作符
与操作符左侧的操作数类型保持一致

  1. static void Main(string[] args)
  2. {
  3. long x = 100;
  4. Console.WriteLine((x<<2).GetType ().FullName );
  5. }

is操作符
为布尔类型
as操作符
as成功,类型与操作符右侧的操作数保持一致;
不成功,返回Null;
& ^ |操作符
与操作数保持一致
&& ||操作符
为布尔类型,且操作数需要为布尔类型
??Null合并操作符
表达式的数据类型就是??左侧操作数的数据类型。

  1. static void Main(string[] args)
  2. {
  3. int? x = null;
  4. //int?完整写法:Nullable<int> x =null;
  5. var y = x ?? 100;
  6. Console.WriteLine(y.GetType().FullName);
  7. }

? :条件操作符
由符号左右两侧操作数决定,精度高决定

  1. static void Main(string[] args)
  2. {
  3. var x = 5 > 3 ? 2 : 3.0;
  4. Console.WriteLine(x);
  5. Console.WriteLine(x.GetType().FullName);
  6. }
  7. //在 : 两边的操作数的类型必须可以进行隐式转换

赋值操作符
表达式数据类型与左侧最终数据类型一致。

复合表达式的求值

注意操作符的优先级和同优先级操作符的运算方向

参考C#语言定义文档

仅作参考,不必深究毕竟我们是在学习语言不是去实现这门语言

语句定义

Wikipedia对语句的定义

In computer programming a statement is the smallest standalone (最小的独立元素)element of an imperative programming language which expresses some action to be carried out.A program written in such a language is formed by a sequence of one or more statements.A statement will have internal components(e.g., expressions) .
语句是高级语言的语法——编译语言和机器语言只有指令(高级语言中的表达式对应低级语言中的指令),语句等价于一个或一组有明显逻辑关联的指令。举例:求圆柱体积。

  1. #include<stdio.h>
  2. double getCylindeVolume(double r,double h)
  3. {
  4. double area=3.14159*r*r;
  5. double volume =area*h;
  6. return volume;
  7. }
  8. int main()
  9. {
  10. double result = getCylinderVolume(10,100);
  11. printf("Volume = %f\n",result);
  12. retuen 0
  13. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace CSharpApp
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. double result = GetCylinderVolume(10,100);
  13. Console.WriteLine(result);
  14. }
  15. static double GetCylinderVolume(double r ,double h)
  16. {
  17. double area = 3.1416 * r * r;
  18. double volume =area * h;
  19. return volume;
  20. }
  21. }
  22. }

1542074067087-647f7028-e3e3-445e-9e5c-f11430bc75b3.png
1542074076921-0e06c592-7b5a-4fd1-86d7-5a8cfe7c75f4.png

C# 看汇编输出

  1. 1. 找到编译的 Application![](https://cdn.nlark.com/yuque/0/2018/png/101969/1542074120324-48379e29-07e6-4536-bd9c-c8577acdc817.png#crop=0&crop=0&crop=1&crop=1&height=157&id=xbklX&originHeight=157&originWidth=594&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=594)
  2. 1. 在电脑里面搜索找到 Developer Command Prompt![](https://cdn.nlark.com/yuque/0/2018/png/101969/1542074134249-4681c4a7-8547-47b0-8e9d-5379d949ee7d.png#crop=0&crop=0&crop=1&crop=1&height=181&id=RsMQf&originHeight=181&originWidth=588&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=588)
  3. 1. 输入 ildasm 命令![](https://cdn.nlark.com/yuque/0/2018/png/101969/1542074150290-521061c4-7044-4161-b92a-f42bab624cc8.png#crop=0&crop=0&crop=1&crop=1&height=230&id=R8EMQ&originHeight=295&originWidth=1025&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=799)
  4. 1. ildasm 工具打开第 1 步找到的 Application![](https://cdn.nlark.com/yuque/0/2018/png/101969/1542074164208-52cda7f8-6338-4d61-9f9c-cddac7804365.png#crop=0&crop=0&crop=1&crop=1&height=591&id=Bgp1s&originHeight=591&originWidth=382&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=382)
  5. 1. 双击具体方法,查看编译结果![](https://cdn.nlark.com/yuque/0/2018/png/101969/1542074179651-69139f4e-f492-470f-9696-5230f328af67.png#crop=0&crop=0&crop=1&crop=1&height=480&id=dbepY&originHeight=602&originWidth=1003&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=799)

C#语言对语句的定义

The actions that a program takes are expressed in statements.Common actions include declaring variables, assigning values, calling methods, looping through collections, and branching to one or another block of code, depending on a given condition.The order in which statements are executed in a program is called the flow of control or flow of execution. The flow of control may vary everytime that a program is run, depending on how the program reacts to input that it receives at runtime.
C#语言的语句除了能够让程序员”顺序地”(sequentially) 表达算法思想, 还能通过条件判断、跳转和循环等芳法控制程序逻辑的走向
简言之就是:陈述算法思想, 控制逻辑走向, 完成有意义的动作(action)
C#语言的语句由分号(;)结尾,俱由分号结尾的不一定都是语句
语句一定是出现在方法体里

  1. using system; //指令
  2. class Student
  3. {
  4. public string name; //字段的声明
  5. }

实例演示控制流(flow of control)

  1. static void Main(string[] args)
  2. {
  3. string input = Console.ReadLine();
  4. try
  5. {
  6. double score = double.Parse(input);
  7. if (score >= 60)
  8. {
  9. Console.WriteLine("Pass!");
  10. }
  11. else
  12. {
  13. Console.WriteLine("Failed!");
  14. }
  15. }
  16. catch
  17. {
  18. Console.WriteLine("Not a number!");
  19. }
  20. }

程序没变,控制流变了。
13141516表达式,语句讲解 - 图3

语句详解

statement:

labeled-statement 标签语句,用得不多

declaration-statement (声明语句)

  1. - 局部变量声明

数据类型:type、var

  1. int x=100;
  2. var y =2; //var声明变量之后,无法更改数据类型
  3. int x
  4. x =100
  5. int x = 100
  6. 两者具有差异,前者没有使用初始化器,适用赋值对x进行更改;后者是使用初始化器
  1. - 局部常量声明

常量表示声明并且初始化之后不能更改的数据;

  1. namespace world
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. const int x = 200;
  8. x = 500; //此处运行会报错,原因是由于x不是变量;一旦常量初始化之后
  9. //就不能更改该常量的数值。
  10. const int y //此处运行会报错,原因是由于声明x常量的同时需要对y使用初始化器
  11. }
  12. }
  13. }

embedded-statement 嵌入式语句

embedded-statement:

block(块语句)

:::info block用于在只允许使用单个语句的上下文中编写多条语句。
block:
{statement-listopt}
block由一个扩在大括号内的可选statement-list组成。如果没有此语句列表, 则称块是空的。
块可以包含声明语句。在一个块中声明的局部变量或常量的范围就是该块本身。

:::

  1. static void Main(string[] args)
  2. {
  3. int x = 100;
  4. {
  5. Console.WriteLine(x);
  6. int y = 200;
  7. Console.WriteLine(y);
  8. }
  9. // Error CS0103 当前上下文中不存在名称“y”
  10. Console.WriteLine(y);
  11. }

:::info 在块内,在表达式上下文中使用的名称的含义必须始终相同。
块按下述规则执行:
如果块是空的,控制转到块的结束点。
如果块不是空的,控制转到语句列表。当控制到达语句列表的结束点时,控制转到块的结束点。

如果块本身是可到达的,则块的语句列表是可到达的。
如果块是空的或者如果语句列表的结束点是可到达的,则块的结束点是可到达的。

包含一条或多条yield语句的block称为迭代器块。迭代器块用于以迭代器的形式实现函数成员。某些附加限制适用于迭代器块:
迭代器块中出现return语句时会产生编译时错误(但允许yie1d return语句) 。
迭代器块包含不安全的上下文(第18.1节)时将导致编译时错误。迭代器块总是定义安全的上下文,即使其定义嵌套在不安全的上下文中也如此。 :::

  • 块语句无论什么时候都被编译器当做一条语句来看待
  • 编译器认为块语句是一条完整的语句(即块语句最后不用加;号)

    empty-statement(空语句)

    expression-statement(表达式语句)

    1. static void Main(string[] args)
    2. {
    3. Console.WriteLine("hello world"); //方法调用表达式+“;”形成语句
    4. new Programing(); //使用new操作符创建对象+“;”形成语句
    5. int x;
    6. x = 100; //赋值表达式+“;”形成语句
    7. x++;
    8. x--;
    9. ++x;
    10. --x; //前增减、后增减+“;”形成语句
    11. }

    expression-statement 用于计算所给定的表达式。由此表达式计算出来的值(如果有)被丢弃。 ```csharp static void Main(string[] args) { // Add 产生的 7.0,如果前面没有拿变量接收它,值被丢弃了。 Add(3.0, 4.0); }

static double Add(double a,double b) { return a + b; }

  1. 不是所有的表达式都允许作为语句使用。具体而言,不允许像x+yx==1这样只计算一个值(此值将被放弃)的表达式作为语句使用。<br />执行一个expression-statement就是对包含的表达式进行计算, 然后将控制转到该expression-statement的结束点。如果一个expression-statement是可到达的, expression-statement结束点也是可到达的。<br />**Single Responsibility** 单一职责原则:一个方法尽量只做一件事情。
  2. <a name="AorkU"></a>
  3. ### selection-statement(选择为语句)
  4. 选择语句会根据表达式的值从若看个给定的语句中选择一个来执行。
  5. <a name="ssDtv"></a>
  6. #### `if`语句
  7. ![1542074514803-0cf8bca8-eafb-498f-bfbb-a226cc3a7006.png](https://cdn.nlark.com/yuque/0/2022/png/26207277/1645769303293-573c4b63-069e-4410-bda3-8fc2ab19bb39.png#clientId=ube940525-d288-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=u84313260&margin=%5Bobject%20Object%5D&name=1542074514803-0cf8bca8-eafb-498f-bfbb-a226cc3a7006.png&originHeight=402&originWidth=825&originalType=binary&ratio=1&rotation=0&showTitle=false&size=202845&status=done&style=none&taskId=u9602df6f-d65d-47a5-93e7-4affb72c464&title=)<br />无论多长的 `if else`,它都是一条语句。之所以能有 `else if{}` 这种结构,也是因为 `if{}` 是一条语句。
  8. <a name="Xp5s0"></a>
  9. #### `switch`语句
  10. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/26207277/1645769519397-0bcb6a23-61ae-4a26-9d15-1afb9403dea7.png#clientId=ube940525-d288-4&crop=0&crop=0&crop=1&crop=1&from=drop&id=u662d5a91&margin=%5Bobject%20Object%5D&name=image.png&originHeight=504&originWidth=825&originalType=binary&ratio=1&rotation=0&showTitle=false&size=257813&status=done&style=none&taskId=u87031ecc-c94d-42e2-9b80-ed429074ddd&title=)
  11. > **注**:从 C# 7.0 开始 switch 表达式已[支持任何非 null 表达式](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/switch#the-match-expression)。
  12. ```csharp
  13. using System;
  14. namespace world
  15. {
  16. class Program
  17. {
  18. static void Main(string[] args)
  19. {
  20. double score = 99;
  21. switch ((int)(score / 10))
  22. {
  23. case 10:
  24. if (score == 100)
  25. {
  26. goto case 8;
  27. }
  28. else
  29. {
  30. goto default;
  31. }
  32. case 9:
  33. //Console.WriteLine("A");两个标签内的语句不能相同,需要相同输出时,可如同7./6.示例
  34. case 8:
  35. Console.WriteLine("A");
  36. break;//C#中标签条件内不能丢失break语句.一旦有了具体的 section,就必需配套 break。
  37. case 7:
  38. case 6:
  39. Console.WriteLine("B");
  40. break;
  41. case 5:
  42. case 4:
  43. Console.WriteLine("C");
  44. break;
  45. case 3:
  46. case 2:
  47. case 1:
  48. case 0:
  49. Console.WriteLine("D");
  50. break;
  51. default://效果类似于if else中的else
  52. break;
  53. }
  54. }
  55. }
  56. }

iteration-statement(迭代/循环语句)

while语句

  • while 语句按不同条件执行一个嵌入语句零次或多次 ```csharp using System; using System.Collections.Generic;

namespace StatementsExample { class Program { static void Main(string[] args) { int score = 0; bool canContiue = true; while (canContiue) { Console.WriteLine(“Please input the first number”); string str1=Console.ReadLine(); int x=int.Parse(str1);

  1. Console.WriteLine("Please input the second number");
  2. string str2 = Console.ReadLine();
  3. int y = int.Parse(str2);
  4. int sum = x + y;
  5. if(sum ==100)
  6. {
  7. score++;
  8. Console.WriteLine("correct!{0}+{1}={2}",x,y,sum);
  9. }
  10. else
  11. {
  12. Console.WriteLine("Error!{0}+{1}={2}",x,y,sum);
  13. canContiue = false;
  14. }
  15. }
  16. Console.WriteLine("your score is {0}",score);
  17. Console.WriteLine("good game!");
  18. }
  19. }

}

  1. <a name="o5NtC"></a>
  2. #### `do`语句
  3. - do 语句按不同条件执行一个嵌入语句一次或多次
  4. 代码见`[break、continue语句](https://www.yuque.com/dingxuchen/cc9wwf/wezf1n/edit)`
  5. <a name="EJNRJ"></a>
  6. #### `for`语句
  7. - for 语句计算一个初始化表达式序列,然后,当某个条件为真时,重复执行相关的嵌入语句并计算一个迭代表达式序列
  8. ```csharp
  9. using System;
  10. using System.Collections.Generic;
  11. namespace StatementsExample
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. for (int i = 1; i <=9; i++)
  18. {
  19. for (int j = 1; j <=i; j++)
  20. {
  21. Console.Write("{0}*{1}={2}\t", i, j, i * j);
  22. }
  23. Console.WriteLine();
  24. }
  25. }
  26. }
  27. }

foreach语句

  • foreach 语句用于枚举一个集合的元素,并对该集合中的每个元素执行一次相关的嵌入语句

1542074801508-044519fd-820e-4a8f-9046-465136b79a44.png
1542074806841-1caa32b0-13e2-461b-9b23-37813054caef.png
1542074812857-6546cbdd-72b9-4f40-b688-631fa3d44657.png
集合遍历的底层原理和迭代器,foreach 语句就是对集合遍历的简记法。

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace StatementsExample
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] intArray = new int[] { 1, 2, 3, 4, 5, 6 };
  11. // Console.WriteLine(intArray.GetType().FullName);
  12. // Console.WriteLine((intArray is Array));
  13. IEnumerator enumerator = intArray.GetEnumerator();//指月
  14. while(enumerator.MoveNext())
  15. {
  16. Console.WriteLine(enumerator.Current);
  17. }
  18. enumerator.Reset();//重复输出解决办法
  19. while(enumerator.MoveNext()) // 重复使用迭代器进行输出,
  20. //结果任然为一次循环输出,原因是因为第一次输出后, 指针已经指向了最后一个数,无法跳转到
  21. //第一个数进行重复输出
  22. {
  23. Console.WriteLine(enumerator.Current);
  24. }
  25. //List<int> intList = new List<int> { 1, 2, 3, 4, 5, 6 };
  26. }
  27. }
  28. }
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace StatementsExample
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] intArray = new int[] { 1, 2, 3, 4, 5, 6 };
  11. List<int> intList = new List<int>(){ 1,2,3,4,5,6};
  12. foreach (var current in intList)
  13. {
  14. Console.WriteLine(current);
  15. }
  16. }
  17. }
  18. }

jump-statement(跳转语句)

continue语句

  • continue 语句将开始直接封闭它的 while、do、for 或 foreach 语句的一次新迭代

    break语句

  • break 语句将退出直接封闭它的 switch、while、do、for 或 foreach 语句 ```csharp using System; using System.Collections.Generic;

namespace StatementsExample { class Program { static void Main(string[] args) { int score = 0; int sum = 0; do { Console.WriteLine(“Please input the first number”); string str1 = Console.ReadLine(); int x=0; if (str1.ToLower() ==”end”|| str1.ToLower() == “END”) { break; } try { x = int.Parse(str1); } catch { Console.WriteLine(“First number has probleam!Restart”); continue; } Console.WriteLine(“Please input the second number”); string str2 = Console.ReadLine(); int y=0; if (str2.ToLower() == “end” || str1.ToLower() == “END”) { break; } try { y = int.Parse(str2); } catch { Console.WriteLine(“Second number has probleam!Restart”); continue; } sum = x + y; if (sum == 100) { score++; Console.WriteLine(“correct!{0}+{1}={2}”, x, y, sum); } else { Console.WriteLine(“Error!{0}+{1}={2}”, x, y, sum);
} }while(sum==100); Console.WriteLine(“your score is {0}”, score); Console.WriteLine(“good game!”); } } }

  1. <a name="N2gDW"></a>
  2. #### `goto`语句
  3. - goto 语句将控制转到由标签标记的语句
  4. - goto 语句基本被淘汰
  5. <a name="nnhZq"></a>
  6. #### `throw`语句
  7. - throw 语句将引发一个异常
  8. - throw 语句语法比较灵活,它后面可以什么都不跟
  9. <a name="vyeZ8"></a>
  10. #### `return`语句
  11. - return 语句会将控制返回到出现 return 语句的函数的当前调用方
  12. - 提前 return 原则
  13. - 方法的每个分支里面都需要有 return
  14. **提前return原则**<br />通过提前 return 可以让代码阅读者立刻就鉴别出来程序将在什么情况下 return,同时减少 if else 嵌套,写出更优雅的代码。
  15. ```csharp
  16. class Program
  17. {
  18. static void Main(string[] args)
  19. {
  20. Greeting("Mr.Duan");
  21. }
  22. static void Greeting(string name)
  23. {
  24. if (string.IsNullOrEmpty(name))
  25. {
  26. // 通过尽早 return 可以让代码阅读者立刻就鉴别出来
  27. // name 参数在什么情况下是有问题的
  28. return;
  29. }
  30. Console.WriteLine("Hello, {0}", name);
  31. }
  32. }

try-statement(try…catch…finally语句)

:::info try语句提供一种机制, 用于捕捉在块的执行期间发生的各种异常。此外, try语句还能让您指定一个代码块, 并保证当控制离开try语句时, 总是先执行该代码
try-statement:
try block catch-clauses
try block finally-clause
try block catch-clauses finally-clause
catch-clauses:
specific-catch-clauses general-catch-clause opt
specific-catch-clausesopr general-catch-clause
specific-catch-clauses:
specific-catch-clause
specific-catch-clauses specific-catch-clause
specific-catch-clause:
catch( class-type identifieropt)block
general-catch-clause:
catch block
finally-clause:
finally block
有三种可能的try语句形式:
一个try块后接一个或多个catch块。
一个try块后接一个finally块。
一个try块后接一个或多个catch块, 后面再跟一个finally块。 ::: 可以通过 MSDN 查方法相应的异常。
如 Int32.Parse 方法 (String) 就有以下异常。
1542074761770-1220bca6-cc26-45c3-9ebd-628761c66f7e.png

  1. - 应该把释放系统资源的语句写在 finally block 里面
  2. - 有时候也在 finally block 里面写 log
  1. using System;
  2. using System.Collections.Generic;
  3. namespace StatementsExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Calculator c = new Calculator();
  10. int r = c.Add("999999999999", "200");
  11. Console.WriteLine(r);
  12. }
  13. class Calculator
  14. {
  15. public int Add(string arg1,string arg2)
  16. {
  17. int a = 0;
  18. int b = 0;
  19. bool hasError = false;
  20. try
  21. {
  22. //int a = int.Parse(arg1);
  23. //int b = int.Parse(arg2);
  24. //局部变量的数据范围不会超出语句块,当使用上述注释语句块,结果为0
  25. //因此需要在try语句块之外对变量进行声明
  26. a =int.Parse(arg1);
  27. b=int.Parse(arg2);
  28. }
  29. catch (FormatException ane)//此处"ane"变量是对FormatException进行调用
  30. {
  31. Console.WriteLine("your arguments are not number");
  32. Console.WriteLine(ane.Message);//当触发此异常时,会对错误进行详细报错
  33. hasError = true;
  34. }
  35. catch(OverflowException)
  36. {
  37. Console.WriteLine("out of range!");
  38. hasError = true;
  39. }
  40. catch(ArgumentException)
  41. {
  42. Console.WriteLine("your arguments are null");
  43. hasError = true;
  44. }
  45. catch//在catch子句之后()内不填写任何类型,说明是通用异常,使用其他说明是针对某一类型的异常进行报错
  46. {
  47. Console.WriteLine("your argunments are error");
  48. hasError = true;
  49. }
  50. finally//不管上述try语句是否执行,是否发生异常,最终都会执行finally语句
  51. {
  52. //会申请系统资源,如释放资源
  53. //书写程序记录
  54. if(hasError)
  55. {
  56. Console.WriteLine("Execution has error");
  57. }
  58. else
  59. {
  60. Console.WriteLine("Done!");
  61. }
  62. //优势:当程序执行时,出现是否发生异常提示,若未发生异常,则程序执行结果为正常结果
  63. }
  64. int result = a + b;
  65. return result;
  66. }
  67. }
  68. }
  69. }

throw

throw 将异常抛给调用者。
throw 关键字的语法比较灵活

  1. try
  2. {
  3. ...
  4. }
  5. catch(OverflowException)
  6. {
  7. throw;
  8. }

checked-statement(checked语句)

unchecked-statement(unchecked语句)

lock-statement(lock语句)

多用于线程

using-statement(using语句)

yield-statement(yield语句)