Priority of operators in C#: unary operator () (positive sign), - (negative sign), ! (logical NOT), ~ (bitwise NOT) * (multiplication), / (division) ), % (remainder) (addition), - (subtraction) < (less than), <= (less than or equal to), > (greater than), >= (greater than or equal to), == (equal to) , != (not equal) && (logical AND), || (logical OR), ^ (logical exclusive OR)?:: (condition? true_expression : false_expression)= (assignment), =, -=, *=, / = (compound assignment)
The precedence of operators in C
#In C#, the precedence of operators The level determines the order in which expressions are evaluated. The precedence of operators from high to low is:
Unary operator
()
: Parentheses
: Positive sign-
: Negative sign!
: Logical negation~
: Bitwise notationMultiplication and division operators
*
: Multiplication/
: Division%
: RemainderAddition and subtraction operators
: Addition-
: SubtractionComparison operator
: less than
: less than or equal to
: greater than
: greater than or equal to
: equal to
: Not equal to
Logical operator
-=
, *=
, /=
: Compound assignment
The following example illustrates how operator precedence affects the evaluation of an expression:
<code class="csharp">int x = 1 + 2 * 3; // 7 int y = (1 + 2) * 3; // 9</code>
In the first expression, multiplication operator has higher precedence than the addition operator, so the expression first evaluates
2 * 3 before adding the result to 1
. In the second expression, the parentheses have higher precedence than the multiplication operator, so the expression first evaluates
and then compares the result with 3
Multiply.
The above is the detailed content of Operator precedence in c#. For more information, please follow other related articles on the PHP Chinese website!