C# 运算符C# operators

C# 提供了许多由内置类型支持的预定义运算符。C# provides a number of predefined operators supported by the built-in types. 例如,算术运算符使用内置数值类型的操作数执行算术运算,布尔逻辑运算符使用 bool 操作数执行逻辑运算。For example, arithmetic operators perform arithmetic operations with operands of built-in numeric types and Boolean logical operators perform logical operations with the bool operands.

用户定义类型可以重载某些运算符来定义该类型的操作数的相应行为。A user-defined type can overload certain operators to define the corresponding behavior for the operands of that type. 有关详细信息,请参阅运算符关键字一文。For more information, see the operator keyword article.

以下各部分按最高优先级到最低优先级的顺序列出 C# 运算符。The following sections list the C# operators starting with the highest precedence to the lowest. 各章节内运算符的优先级相同。The operators within each section share the same precedence level.

主要运算符Primary operators

以下是具有最高优先级的运算符。These are the highest precedence operators.

x.y:成员访问。x.y – member access.

x?.y:null 条件成员访问。x?.y – null conditional member access. 如果左操作数计算结果为 null,则返回 nullReturns null if the left-hand operand evaluates to null.

x?[y]:null 条件数组元素或类型索引器访问。x?[y] - null conditional array element or type indexer access. 如果左操作数计算结果为 null,则返回 nullReturns null if the left-hand operand evaluates to null.

f(x):方法调用或委托调用。f(x) – method call or delegate invocation.

a[x]:数组元素或类型索引器访问。a[x] – array element or type indexer access.

x++:后缀递增。x++ – postfix increment. 先返回 x 值,然后用加 1(通常加整数 1)后的 x 值更新存储位置。Returns the value of x and then updates the storage location with the value of x that is one greater (typically adds the integer 1).

x--:后缀递减。x-- – postfix decrement. 先返回 x 值,然后用减 1(通常减整数 1)后的 x 值更新存储位置。Returns the value of x and then updates the storage location with the value of x that is one less (typically subtracts the integer 1).

new:类型实例化。new – type instantiation.

typeof - 返回表示操作数的 Type 对象。typeof – returns the Type object representing the operand.

checked:对整数运算启用溢出检查。checked – enables overflow checking for integer operations.

unchecked:对整数运算禁用溢出检查。unchecked – disables overflow checking for integer operations. 这是默认的编译器行为。This is the default compiler behavior.

default(T) - 生成类型 T 的默认值。default(T) – produces the default value of type T.

nameof:获取变量、类型或成员的简单(非限定)名称作为常数字符串。nameof - obtains the simple (unqualified) name of a variable, type, or member as a constant string.

delegate:声明并返回委托实例。delegate – declares and returns a delegate instance.

sizeof:返回类型操作数的大小(以字节为单位)。sizeof – returns the size in bytes of the type operand.

stackalloc:在堆栈上分配内存块。stackalloc - allocates a block of memory on the stack.

->:指针间接寻址与成员访问相结合。-> – pointer indirection combined with member access.

一元运算符Unary operators

这些运算符的优先级比下一章节高,比上一章节低。These operators have higher precedence than the next section and lower precedence than the previous section.

+x:返回 x 的值。+x – returns the value of x.

-x:数值取反。-x – numeric negation.

!x:逻辑取反。!x – logical negation.

~x:按位求补。~x – bitwise complement.

++x:前缀递增。++x – prefix increment. 先用加 1(通常加整数 1)后的 x 值更新存储位置,然后返回 x 值。Returns the value of x after updating the storage location with the value of x that is one greater (typically adds the integer 1).

--x:前缀递减。--x – prefix decrement. 先用减 1(通常减整数 1)后的 x 值更新存储位置,然后返回 x 值。Returns the value of x after updating the storage location with the value of x that is one less (typically subtracts the integer 1).

(T)x:类型显式转换。(T)x – type casting.

await:等待 Taskawait – awaits a Task.

&x:变量的地址。&x – address of a variable.

*x:指针间接寻址或取消引用。*x – pointer indirection, or dereference.

true 运算符 - 返回 booltrue 以指明操作数一定为 true。true operator - returns the bool value true to indicate that an operand is definitely true.

false 运算符 - 返回 booltrue 以指明操作数一定为 false。false operator - returns the bool value true to indicate that an operand is definitely false.

乘法运算符Multiplicative operators

这些运算符的优先级比下一章节高,比上一章节低。These operators have higher precedence than the next section and lower precedence than the previous section.

x * y:乘法。x * y – multiplication.

x / y:除法。x / y – division. 如果操作数均为整数,则结果为整数,舍去小数(例如,-7 / 2 is -3)。If the operands are integers, the result is an integer truncated toward zero (for example, -7 / 2 is -3).

x % y:余数。x % y – remainder. 如果操作数均为整数,则返回 x 除以 y 后的余数。If the operands are integers, this returns the remainder of dividing x by y. 如果 q = x / yr = x % y,则 x = q * y + rIf q = x / y and r = x % y, then x = q * y + r.

相加运算符Additive operators

这些运算符的优先级比下一章节高,比上一章节低。These operators have higher precedence than the next section and lower precedence than the previous section.

x + y:加法。x + y – addition.

x - y:减法。x – y – subtraction.

移位运算符Shift operators

这些运算符的优先级比下一章节高,比上一章节低。These operators have higher precedence than the next section and lower precedence than the previous section.

x << y:左移位,右侧用 0 填充。x << y – shift bits left and fill with zero on the right.

x >> y:右移位。x >> y – shift bits right. 如果左操作数是 intlong,则左位数补符号位。If the left operand is int or long, then left bits are filled with the sign bit. 如果左操作数是 uintulong,则左位数补零。If the left operand is uint or ulong, then left bits are filled with zero.

关系和类型测试运算符Relational and type-testing operators

这些运算符的优先级比下一章节高,比上一章节低。These operators have higher precedence than the next section and lower precedence than the previous section.

x < y:小于(如果 x 小于 y,则为 true)。x < y – less than (true if x is less than y).

x > y:大于(如果 x 大于 y,则为 true)。x > y – greater than (true if x is greater than y).

x <= y:小于或等于。x <= y – less than or equal to.

x >= y:大于或等于。x >= y – greater than or equal to.

is:类型兼容性。is – type compatibility. 如果求值后的左操作数可以转换为右操作数中指定的类型(静态类型),则返回 true。Returns true if the evaluated left operand can be cast to the type specified in the right operand (a static type).

as:类型转换。as – type conversion. 返回左操作数并转换为右操作数中指定的类型(静态类型),但 as 返回 null,其中 (T)x 会引发异常。Returns the left operand cast to the type specified by the right operand (a static type), but as returns null where (T)x would throw an exception.

相等运算符Equality operators

这些运算符的优先级比下一章节高,比上一章节低。These operators have higher precedence than the next section and lower precedence than the previous section.

x == y:相等。x == y – equality. 默认情况下,对于 string 以外的引用类型,此运算符返回引用相等(标识测试)。By default, for reference types other than string, this returns reference equality (identity test). 但是,类型可以重载 ==,因此,如果你想测试标识,最好对 object 使用 ReferenceEquals 方法。However, types can overload ==, so if your intent is to test identity, it is best to use the ReferenceEquals method on object.

x != y:不相等。x != y – not equal. 请参阅有关 == 的注释。See comment for ==. 如果某个类型重载 ==,则它必须重载 !=If a type overloads ==, then it must overload !=.

逻辑 AND 运算符Logical AND operator

此运算符的优先级比下一章节高,比上一章节低。This operator has higher precedence than the next section and lower precedence than the previous section.

x & y - bool 操作数的逻辑与或整型类型操作数的位逻辑与x & ylogical AND for the bool operands or bitwise logical AND for the operands of the integral types.

辑 XOR 运算Logical XOR operator

此运算符的优先级比下一章节高,比上一章节低。This operator has higher precedence than the next section and lower precedence than the previous section.

x ^ y - bool 操作数的逻辑异或或整型类型操作数的位逻辑异或x ^ ylogical XOR for the bool operands or bitwise logical XOR for the operands of the integral types.

逻辑 OR 运算符Logical OR operator

此运算符的优先级比下一章节高,比上一章节低。This operator has higher precedence than the next section and lower precedence than the previous section.

x | y - bool 操作数的逻辑或或整型类型操作数的位逻辑或x | ylogical OR for the bool operands or bitwise logical OR for the operands of the integral types.

条件 AND 运算符Conditional AND operator

此运算符的优先级比下一章节高,比上一章节低。This operator has higher precedence than the next section and lower precedence than the previous section.

x && y:逻辑 AND。x && y – logical AND. 如果第一个操作数计算结果为 false,则 C# 不对第二个操作数求值。If the first operand evaluates to false, then C# does not evaluate the second operand.

条件 OR 运算符Conditional OR operator

此运算符的优先级比下一章节高,比上一章节低。This operator has higher precedence than the next section and lower precedence than the previous section.

x || y:逻辑 OR。x || y – logical OR. 如果第一个操作数计算结果为 true,则 C# 不对第二个操作数求值。If the first operand evaluates to true, then C# does not evaluate the second operand.

Null 合并运算符Null-coalescing operator

此运算符的优先级比下一章节高,比上一章节低。This operator has higher precedence than the next section and lower precedence than the previous section.

x ?? y:如果不为 null,则返回 x;否则返回 yx ?? y – returns x if it is non-null; otherwise, returns y.

条件运算符Conditional operator

此运算符的优先级比下一章节高,比上一章节低。This operator has higher precedence than the next section and lower precedence than the previous section.

t ? x : y - 如果测试 t 计算结果为 true,则计算并返回 x;否则,计算并返回 yt ? x : y – if test t evaluates to true, then evaluate and return x; otherwise, evaluate and return y.

赋值和 lambda 运算符Assignment and lambda operators

这些运算符的优先级比下一章节高,比上一章节低。These operators have higher precedence than the next section and lower precedence than the previous section.

x = y:赋值。x = y – assignment.

x += y:递增。x += y – increment. x 值加 y 值,结果存储在 x 中,并返回新值。Add the value of y to the value of x, store the result in x, and return the new value. 如果 x 指定 event,则 y 必须是 C# 作为事件处理程序添加的相应函数。If x designates an event, then y must be an appropriate function that C# adds as an event handler.

x -= y:递减。x -= y – decrement. x 值减 y 值,结果存储在 x 中,并返回新值。Subtract the value of y from the value of x, store the result in x, and return the new value. 如果 x 指定 event,则 y 必须是 C# 作为事件处理程序删除的相应函数。If x designates an event, then y must be an appropriate function that C# removes as an event handler.

x *= y:乘法赋值。x *= y – multiplication assignment. x 值乘以 y 值,结果存储在 x 中,并返回新值。Multiply the value of y to the value of x, store the result in x, and return the new value.

x /= y:除法赋值。x /= y – division assignment. x 值除以 y 值,结果存储在 x 中,并返回新值。Divide the value of x by the value of y, store the result in x, and return the new value.

x %= y:余数赋值。x %= y – remainder assignment. x 值除以 y 值,余数存储在 x 中,并返回新值。Divide the value of x by the value of y, store the remainder in x, and return the new value.

x &= y:AND 赋值。x &= y – AND assignment. y 值和 x 值相与,结果存储在 x 中,并返回新值。AND the value of y with the value of x, store the result in x, and return the new value.

x |= y:OR 赋值。x |= y – OR assignment. y 值和 x 值相或,结果存储在 x 中,并返回新值。OR the value of y with the value of x, store the result in x, and return the new value.

x ^= y:XOR 赋值。x ^= y – XOR assignment. y 值和 x 值相异或,结果存储在 x 中,并返回新值。XOR the value of y with the value of x, store the result in x, and return the new value.

x <<= y:左移赋值。x <<= y – left-shift assignment. x 值向左移动 y 位,结果存储在 x 中,并返回新值。Shift the value of x left by y places, store the result in x, and return the new value.

x >>= y:右移赋值。x >>= y – right-shift assignment. x 值向右移动 y 位,结果存储在 x 中,并返回新值。Shift the value of x right by y places, store the result in x, and return the new value.

=>:lambda 声明。=> – lambda declaration.

请参阅See also