表达式由操作数 (operand) 和运算符 (operator) 构成。表达式的运算符指示对操作数适用什么样的运算。运算符的示例包括+、-、*、/ 和 new。操作数的示例包括文本、字段、局部变量和表达式。
当表达式包含多个运算符时,运算符的优先级 (precedence) 控制各运算符的计算顺序。例如,表达式 x + y * z 按 x + (y * z) 计算,因为 * 运算符的优先级高于 + 运算符。
大多数运算符都可以重载 (overload)。运算符重载允许指定用户定义的运算符实现来执行运算,这些运算的操作数中至少有一个,甚至所有操作数都属于用户定义的类类型或结构类型。
下表总结了 C# 运算符,并按优先级从高到低的顺序列出各运算符类别。同一类别中的运算符优先级相同。
类别 | 表达式 | 说明 |
基本 | x.m | 成员访问 |
x(...) | 方法和委托调用 | |
x[...] | 数组和索引器访问 | |
x++ | 后增量 | |
x-- | 后减量 | |
new T(...) | 对象和委托创建 | |
new T(...){...} | 使用初始值设定项创建对象 | |
new {...} | 匿名对象初始值设定项 | |
new T[...] | 数组创建 | |
typeof(T) | 获取 T 的 System.Type 对象 | |
checked(x) | 在 checked 上下文中计算表达式 | |
unchecked(x) | 在 unchecked 上下文中计算表达式 | |
default(T) | 获取类型 T 的默认值 | |
delegate {...} | 匿名函数(匿名方法) | |
一元 | +x | 恒等 |
-x | 求相反数 | |
!x | 逻辑求反 | |
~x | 按位求反 | |
++x | 前增量 | |
--x | 前减量 | |
(T)x | 将 x 显式转换为类型 T | |
await x | 异步等待 x 完成 | |
乘法 | x * y | 乘法 |
x / y | 除法 | |
x % y | 求余 |
加减 | x + y | 加法、字符串串联、委托组合 |
x – y | 减法、委托移除 | |
移位 | x << y | 左移 |
x >> y | 右移 | |
关系和类型检测 | x < y | 小于 |
x > y | 大于 | |
x <= y | 小于或等于 | |
x >= y | 大于或等于 | |
x is T | 如果 x 为 T,则返回 true,否则返回 false | |
x as T | 返回转换为类型 T 的 x,如果 x 不是 T 则返回 null | |
相等 | x == y | 等于 |
x != y | 不等于 | |
逻辑“与” | x & y | 整型按位 AND,布尔逻辑 AND |
逻辑 XOR | x ^ y | 整型按位 XOR,布尔逻辑 XOR |
逻辑 OR | x | y | 整型按位 OR,布尔逻辑 OR |
条件 AND | x && y | 仅当 x 为 true 时,才对 y 求值 |
条件 OR | x || y | 仅当 x 为 false 时,才对 y 求值 |
null 合并 | X ?? y | 如果 x 为 null,则计算结果为 y,否则计算结果为 x |
条件 | x ? y : z | 如果 x 为 true,则对 y 求值;如果 x 为 false,则对 z 求值 |
赋值或匿名函数 | x = y | 赋值 |
x op= y | 复合赋值;支持的运算符有: *= /= %= += -= <<= >>= &= ^= |= | |
(T x) => y | 匿名函数(lambda 表达式) |
语句
程序的操作是使用语句 (statement) 来表示的。C# 支持几种不同的语句,其中许多以嵌入语句的形式定义。
block 用于在只允许使用单个语句的上下文中编写多条语句。块由位于一对大括号 { 和 } 之间的语句列表组成。
声明语句 (declaration statement) 用于声明局部变量和常量。
表达式语句 (expression statement) 用于对表达式求值。可用作语句的表达式包括方法调用、使用 new 运算符的对象分配、使用 = 和复合赋值运算符的赋值、使用 ++ 和 -- 运算符的增量和减量运算以及 await 表达式。
选择语句 (selection statement) 用于根据表达式的值从若干个给定的语句中选择一个来执行。这一组中有 if 和 switch 语句。
迭代语句 (iteration statement) 用于重复执行嵌入语句。这一组中有 while、do、for 和 foreach 语句。
跳转语句 (jump statement) 用于转移控制。这一组中有 break、continue、goto、throw、return 和 yield 语句。
try...catch 语句用于捕获在块的执行期间发生的异常,try...finally 语句用于指定终止代码,不管是否发生异常,该代码都始终要执行。
checked 语句和 unchecked 语句用于控制整型算术运算和转换的溢出检查上下文。
lock 语句用于获取某个给定对象的互斥锁,执行一个语句,然后释放该锁。
using 语句用于获得一个资源,执行一个语句,然后释放该资源。
下表列出了 C# 的各语句,并提供每个语句的示例。
语句 | 示例 |
局部变量声明 | static void Main() { |
局部常量声明 | static void Main() { |
表达式语句 | static void Main() { |
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() { |
continue语句 | static void Main(string[] args) { |
goto语句 | static void Main(string[] args) { |
return语句 | static int Add(int a, int b) { static void Main() { |
yield语句 | static IEnumerable<int> Range(int from, int to) { static void Main() { |
throw 和 try | static double Divide(double x, double y) { static void Main(string[] args) { |
checked 和 unchecked 语句 | static void Main() { |
lock语句 | class Account public void Withdraw(decimal amount) { |
using语句 | static void Main() { |