Precedence of C # operators

Source: Internet
Author: User

When an expression contains multiple operators, the precedence of the operator controls the order in which a single operator is evaluated. For example, an expression x+y*z is evaluated by x+ (Y*Z) because the "*" operator has a higher priority than the "+" operator. This is consistent with the multiplication and subtraction of mathematical operations.

Table 7-1 summarizes the order of precedence for all operators from highest to lowest.

Table 7-1 Precedence order of operators from highest to lowest

Category Operator
Primary operator (x) x.y f (x) a[x] x + + x--new type of sizeof checked unchecked
Unary operator + - ! ~ ++x--x (T) x
Multiply, divide operator * / %
Add and subtract operator + -
Shift operator << >>
Relational operators < > <= >= is as
Equality operator == !=
Logic and operators &
Logical XOR OR operator ^
Logical OR operator |
Conditions and operators &&
Condition or operator ||
Conditional operator ?:
Assignment operator = *=/=%= + = = <<= >>= &= ^= |=

When an operand appears between two operators with the same priority, the operators are executed from left to right in the order in which they appear.

In addition to assignment operators, all binary operators are left-bound (left-associative), that is, actions are performed in Left-to-right order. For example: X+y+z Press (X+Y) +z for evaluation.

Assignment operators and conditional operators (?:) According to the principle of right engagement (right-associative), operations are performed in Right-to-left order. such as: X=y=z in accordance with x= (Y=Z) for evaluation.

It is recommended that when writing an expression, if you cannot determine the effective order of the operators, then try to use parentheses to ensure the order of operations, so that the program at a glance, and their own programming can be clear thinking.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.