SoFunction
Updated on 2025-03-06

Dividing and understanding of the priority of 38 commonly used operators in C#

In C#, there are 38 commonly used characters. According to the characteristics of the operations they perform and their priorities, for the sake of memory, I classify them into seven levels: 1. Unit operators and brackets. 2. Regular arithmetic operators. 3. Displacement operator. 4. Comparison operator. 5. Logical operators. 6. Various assignment operators. 7. Right bit (suffix) unit operator.

1. In this level, there are ++, -- (as prefix), (), +, - (as unit operators), !, ~. This level is full of unit operators, except for the special pair of brackets that change the priority of any operation. This shows that in the definition expression, the priority of those unit operators is very high, perhaps because they all act directly on the operand. Only two unit operators have no priority in the first level, and they appear behind for special reasons.

2. Among the conventional arithmetic operators, there are *, /, %, +, and - that we often use, because they use it more, so they are also ranked higher.

3. This level is two special bit operators, << and >>, which are the highest priority among all binary operators except for conventional operators. It may be because compared with comparison operators and logical operators, this set of operators still performs numerical calculations.

4. Comparison operators, including <, >, <=, >=, ==, !=, there are six characteristics here, which are less than or greater than.

5. Logical operators. There were originally four logical operators, but the "non" operators used for units were ranked first, so there are only &, ^, and |, plus two additional operators &&, || to improve code efficiency, and there are five in total.

6. Assignment operators, the most level is at the moment. Almost the binary operators that appeared before, add a "=" sign here to become an assignment operator. First of all, of course, the most basic assignment operator "="; then the "*=, /=, %=, +=, -=" evolved from the conventional arithmetic operators, and their order is the same as that of the conventional arithmetic; then the displacement operators and logical operators, which are also arranged in the order before their evolution, as "<<=, >>=, &=, ^=, |=". Because the data type of the result produced by the comparison operator is different from the data type of its operand, they do not have corresponding assignment operators.

7. The last level is the ++ and -- of the two last unit operators suffix versions. They appear to supplement the two prefixed versions == and -- (the two have too high priority, and people need two less priority) to facilitate the design of expressions, so the priority of these two must of course be ranked last.

Then, the priority order of these 38 operators can be written as follows: (Whether horizontal or vertical, the higher the priority is, the higher the priority)
Level 1: ++, -- (as prefix), (), +, - (as unit operator), !, ~.
Level 2: *, /, %, +, -.
Level 3: <<, >>.
Level 4: <, >, <=, >=, ==, !=.
Level 5: &,^,|,&&,||.
Level 6: =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=.
Level 7: ++, -- (as suffix).

This is a summary of what I have learned in C#. The understanding of the division methods at each level and the priority of each operator is not necessarily correct. I just want to share my own opinions with you.