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.
演算子の結合規則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 合体演算子
??
を除き、2 項演算子はすべて左からの結合です。Except for the assignment operators and the null-coalescing operator??
, all binary operators are left-associative. たとえば、a + b - c
は(a + b) - c
と評価されます。For 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 asx = (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
フィードバック
フィードバックを読み込んでいます...