C# 運算子 (C# 參考)C# operators (C# reference)

C# 提供許多由內建類型支援的運算子。C# provides a number of operators supported by the built-in types. 舉例來說,以數值運算元執行算術運算的算術運算子,以及以布林運算元執行邏輯運算的布林值邏輯運算子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-yf (x)[i] x?.y、、 x?[y]x + +x--x!newtypeofcheckeduncheckeddefaultnameofdelegatesizeofstackallocx->yx.y, f(x), a[i], x?.y, x?[y], x++, x--, x!, new, typeof, checked, unchecked, default, nameof, delegate, sizeof, stackalloc, x->y PrimaryPrimary
+ 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)Unary
x.。yx..y 範圍Range
switchswitch switch 運算式switch expression
x * yx / yx % yx * y, x / y, x % y 乘法Multiplicative
x + yx – yx + y, x – y 加法Additive
< x < yx >> yx << y, x >> y ShiftShift
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 條件式 ANDConditional AND
x || yx || y 條件式 ORConditional OR
x ?? yx ?? y Null 聯合運算子Null-coalescing operator
c ? t : fc ? t : f 條件運算子Conditional operator
x = yx + = yx-= yx * = yx/= yx% = yx &= yx |= y,x ^ =y,x <<= 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 operators, 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 operators, 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. 不過,有些運算子會有條件地評估運算元。However, some operators evaluate operands conditionally. 也就是說,這類運算子最左邊的運算元值會定義是否應該評估其他運算元。That is, the value of the leftmost operand of such an operator defines if (or which) other operands should be evaluated. 這些運算子是條件式邏輯AND (&&OR (||運算子、 null 聯合運算子????= null 條件運算子?. and ?[]條件運算子?: These operators are the conditional logical AND (&&) and OR (||) operators, the null-coalescing operators ?? and ??=, the null-conditional operators ?. and ?[], and the conditional operator ?:. 如需詳細資訊,請參閱每個運算子的說明。For more information, see the description of each operator.

C# 語言規格C# language specification

如需詳細資訊,請參閱 C# 語言規格運算子一節。For more information, see the Operators section of the C# language specification.

請參閱See also