C# 运算符(C# 参考)C# operators (C# reference)

C# 提供了许多由内置类型支持的运算符。C# provides a number of operators supported by the built-in types. 例如,算术运算符使用数值操作数执行算术运算,布尔逻辑运算符使用 bool 操作数执行逻辑运算。For example, arithmetic operators perform arithmetic operations with numeric operands and Boolean logical operators perform logical operations with the bool operands. 特定的运算符可重载Certain operators can be overloaded. 使用运算符重载,可以为用户定义类型的操作数指定运算符行为。With operator overloading, you can specify the operator behavior for the operands of a user-defined type.

表达式中,运算符优先级和结合性决定了操作的执行顺序。In an expression, operator precedence and associativity determine the order in which the operations are performed. 可以使用括号更改由运算符优先级和结合性决定的计算顺序。You can use parentheses to change the order of evaluation imposed by operator precedence and associativity.

运算符优先级Operator precedence

在包含多个运算符的表达式中,先按优先级较高的运算符计算,再按优先级较低的运算符计算。In an expression with multiple operators, the operators with higher precedence are evaluated before the operators with lower precedence. 在下面的示例中,首先执行乘法,因为其优先级高于加法:In the following example, the multiplication is performed first because it has higher precedence than addition:

var a = 2 + 2 * 2;
Console.WriteLine(a); //  output: 6

使用括号更改运算符优先级所施加的计算顺序:Use parentheses to change the order of evaluation imposed by operator precedence:

var a = (2 + 2) * 2;
Console.WriteLine(a); //  output: 8

下表按最高优先级到最低优先级的顺序列出 C# 运算符。The following table lists the C# operators starting with the highest precedence to the lowest. 每行中运算符的优先级相同。The operators within each row have the same precedence.

运算符Operators 类别或名称Category or name
x.yx?.yx?[y]f(x)a[i]x++x--newtypeofcheckeduncheckeddefaultnameofdelegatesizeofstackallocx->yx.y, x?.y, x?[y], f(x), a[i], x++, x--, new, typeof, checked, unchecked, default, nameof, delegate, sizeof, stackalloc, x->y 基本Primary
+x-x!x~x++x--x^x(T)xawait&x*xtrue 和 false+x, -x, !x, ~x, ++x, --x, ^x, (T)x, await, &x, *x, true and false 一元Unary
x..yx..y 范围Range
x * yx / yx % yx * y, x / y, x % y 乘法Multiplicative
x + yx – yx + y, x – y 加法Additive
x << yx >> yx << y, x >> y 移位Shift
x < yx > yx <= yx >= yisasx < y, x > y, x <= y, x >= y, is, as 关系和类型测试Relational and type-testing
x == yx != yx == y, x != y 相等Equality
x & y 布尔逻辑 AND按位逻辑 ANDBoolean logical AND or bitwise logical AND
x ^ y 布尔逻辑 XOR按位逻辑 XORBoolean logical XOR or bitwise logical XOR
x | y 布尔逻辑 OR按位逻辑 ORBoolean logical OR or bitwise logical OR
x && yx && y 条件“与”Conditional AND
x || yx || y 条件“或”Conditional OR
x ?? yx ?? y Null 合并运算符Null-coalescing operator
c ? t : fc ? t : f 条件运算符Conditional operator
x = yx += yx -= yx *= yx /= yx %= yx &= yx |= yx ^= yx <<= yx >>= yx ??= y=>x = y, x += y, x -= y, x *= y, x /= y, x %= y, x &= y, x |= y, x ^= y, x <<= y, x >>= y, x ??= y, => 赋值和 lambda 声明Assignment and lambda declaration

运算符结合性Operator associativity

当运算符的优先级相同,运算符的结合性决定了运算的执行顺序:When operators have the same precedence, associativity of the operators determines the order in which the operations are performed:

  • 左结合运算符按从左到右的顺序计算。 Left-associative operators are evaluated in order from left to right. 赋值运算符null 合并运算符 ??外,所有二元运算符都是左结合运算符。Except for the assignment operators and the null-coalescing operator ??, all binary operators are left-associative. 例如,a + b - c 将计算为 (a + b) - cFor example, a + b - c is evaluated as (a + b) - c.
  • 右结合运算符按从右到左的顺序计算。 Right-associative operators are evaluated in order from right to left. 赋值运算符、null 合并运算符 ??条件运算符 ?:是右结合运算符。The assignment operators, the null-coalescing operator ??, and the conditional operator ?: are right-associative. 例如,x = y = z 将计算为 x = (y = z)For example, x = y = z is evaluated as x = (y = z).

使用括号更改运算符结合性所施加的计算顺序:Use parentheses to change the order of evaluation imposed by operator associativity:

int a = 13 / 5 / 2;
int b = 13 / (5 / 2);
Console.WriteLine($"a = {a}, b = {b}");  // output: a = 1, b = 6

操作数计算Operand evaluation

与运算符的优先级和结合性无关,从左到右计算表达式中的操作数。Unrelated to operator precedence and associativity, operands in an expression are evaluated from left to right. 以下示例展示了运算符和操作数的计算顺序:The following examples demonstrate the order in which operators and operands are evaluated:

表达式Expression 计算顺序Order of evaluation
a + b a, b, +a, b, +
a + b * c a, b, c, *, +a, b, c, *, +
a / b + c * d a, b, /, c, d, *, +a, b, /, c, d, *, +
a / (b + c) * d a, b, c, +, /, d, *a, b, c, +, /, d, *

通常,会计算所有运算符操作数。Typically, all operator operands are evaluated. 某些运算符有条件地计算操作数。Some operators evaluate operands conditionally. 也就是说,此类运算符的第一个操作数的值定义了是否应计算其他操作数,或计算其他哪些操作数。That is, the value of the first operand of such an operator defines if (or which) other operands should be evaluated. 这些运算符有条件逻辑 AND (&&)OR (||) 运算符、null 合并运算符 ??null 条件运算符 ?. 和和 ?[],以及条件运算符 ?:These operators are the conditional logical AND (&&) and OR (||) operators, the null-coalescing operator ??, the null-conditional operators ?. and ?[], and the conditional operator ?:. 有关更多详细信息,请参阅每个运算符的说明。See the description of each operator for more details.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范中的运算符部分。For more information, see the Operators section of the C# language specification.

请参阅See also