C Sharp/Operators

算术

算术运算符的运算数都是数值型的,即下述ab是表达式。

表达式读法元数解释
a + ba plus bbinary加法
a - ba minus bbinary减法
a*ba times bbinary乘法
a/ba divided by bbinary除法。如果2个操作数都是整数,商也是整数。
a%ba mod bbinary要求操作数都是整数,返回余数。
a++a plus plus or Postincrement aunary操作数必须是左值,返回加一之前的值。
++aplus plus a or Preincrement aunary操作数必须是左值,返回加一之后的值。
a--a minus minus or Postdecrement aunary操作数必须是左值,返回减一之前的值。
--aminus minus a or Predecrement aunary操作数必须是左值,返回减一之后的值。

逻辑

逻辑运算符的运算数是布尔值或整数值。

表达式读法元数解释
a&ba bitwise and bbinary对两个操作数均求值,如果是整型则按位与,如果是布尔型则逻辑与。
a&&ba and bbinary只用于布尔值表达式。短路求值做逻辑与。
a | ba bitwise or bbinary对两个操作数均求值,如果是整型则按位或,如果是布尔型则逻辑或。
a || ba or bbinary只用于布尔值表达式。短路求值做逻辑或。
a ^ ba x-or bbinary如果是整型则按位异或,如果是布尔型则逻辑异或。
!anot aunary只用于布尔值表达式。逻辑非
~abitwise not aunary只用于整型值表达式。按位非

按位移位

表达式读法元数解释
a << ba left shift bbinary左移位
a >> ba right shift bbinary右移位

关系

表达式读法元数解释
a == ba is equal to bbinary对于值类型或字符串类型,比较值是否相等。对于引用类型,比较是否为同一对象。
a != ba is not equal to bbinary返回运算符==的逻辑非。
a < ba is less than bbinary只用于数值表达式。小于。
a > ba is greater than bbinary只用于数值表达式。大于。
a <= ba is less than or equal to bbinary只用于数值表达式。小于等于。
a >= ba is greater than or equal to bbinary只用于数值表达式。大于等于。

赋值

表达式读法元数解释
a = ba equals (or set to) bbinary对于值类型,修改左值的基本值;对于引用类型,左值指向新的引用对象。
a = b = cb set to c, and then a set to bn-ary连续赋值。等价于a = (b = c)

短路赋值

表达式读法元数解释
a += ba plus equals (or increment by) bbinary等价于a = a + b.
a -= ba minus equals (or decrement by) bbinary等价于a = a - b.
a *= ba multiply equals (or multiplied by) bbinary等价于a = a*b.
a /= ba divide equals (or divided by) bbinary等价于a = a/b.
a %= ba mod equals bbinary等价于a = a%b.
a &= ba and equals bbinary等价于a = a&b.
a |= ba or equals bbinary等价于a = a|b.
a ^= ba xor equals bbinary等价于a = a^b.
a <<= ba left-shift equals bbinary等价于a = a << b.
a >>= ba right-shift equals bbinaryE等价于a = a >> b.

类型信息

表达式读法元数解释
x is Tis x of type Tbinaryis运算符会忽略在要检查的实例的类上定义的任何运算符重载。当使用==运算符时,该运算符可能会被重载。
x as Tcast x to Tbinary等价于x is T ? (T)x : null
sizeof(x)size of xunary只能用于值类型
typeof(T)type of Tunary返回System.Type对象。操作数必须是类型。对象应该用GetType方法

指针操纵

表达式读法元数解释
*aobject at aunary间址运算符
a->membermember member of abinary类似于.运算符。
a[b]object at offset b from abinary索引一个指针
&areference to aunary取地址
stackalloc aallocate a on the stackbinary栈内存分配
fixed aprevent a from being relocatedbinary

溢出异常控制

表达式读法元数解释
checked(a)evaluate a and check for overflowunary对值a溢出检查
unchecked(a)evaluate a without checking for overflowunary对值a避免溢出检查

其他

表达式读法元数解释
a.bmember b of abinary访问类型或命名空间a的成员b。如b是字段,则调用该字段的get函数
a[b]item b in abinary返回a的索引b的项目的值。数组是基于0索引
a[^b]item b from the end in abinary返回a从尾部计数的索引b的项目的值。C# 8.0的特性。^e是System.Index类型。
a[b..c]binary返回a的索引在[b,c)的项目值列表。C# 8.0的特性。表达式a..b是System.Range类型
(a)bcast b to type abinaryb的值显式转换为类型ab的类型必须有类型转换函数直接到a或者到另一个类型并有转换函数到a.
new acreate a new an-ary创建一个a类型的对象并调用缺省构造函数。a类型的构造函数也可以带实参:new a(type1 arg1, type2 arg2, ...)
a + bconcatenate string b to the end of string abinary如果ab是字符串,则连接它们。如果任何一个操作数是null, 则被空字符串("")替代。如果一个操作数是字符串另一个是非字符串的object, 则连接前调用ToString()
a + bconcatenate delegate b to delegate abinary如果ab是委托,执行委托的连接。
a ? b : cif a then b else cternary如果a是真,则对b求值并返回其值,否则对c求值并返回其值。任何情况bc只有一个求值.
a ?? bif a is null then b else abinary如果anull, 对b求值并返回其值,否则对a求值并返回其值。如果anull, b不会被求值.
a?.bif a is not null then evaluate bbinary在试图引用对象的成员之前,先判断对象是否为null。可连续使用null判断,如:a?.b()??ca?.b()?[c]
a?[b]if a is not null then get bbinary在试图引用数组的元素之前,先判断对象是否为null。可连续使用null判断,如:a?[b]??c)或a?.b()?[c]
@"a"verbatim "a"nullary字符串忽略转义字符序列。
$"{b}"insert b into the string literaln-ary$开始一个字符串插值语句。包含在{}的符号名字将被求出其字符串值,必要时调用ToString()
$@"{b}"insert b into the verbatim string literaln-ary组合@$. 使用单层花括号{ and }。从C# 8开始,运算符可以是任意序。