In c#, Operators Precedence is useful to define multiple operators in a single expression, and the evaluation of expression can be happened based on the priority of operators.
For example, the multiplication operator (*) is having higher precedence than the addition operator (+). So if we use both multiplication (*) and addition (+) operators in a single expression, first, it will evaluate the multiplication part and then the addition part in expression.
Following is the simple example of defining an expression with operator precedence in c#.
When we execute the above statement, the multiplication part (4 * 5) will be evaluated first. After that, the addition part (3 + 12) will be executed, and the i value will become a 23.
As said earlier, the multiplication (*) operator is having higher precedence than the addition (+) operator so that the first multiplication part will be executed.
The following table lists the different types of operators available in c# relational operators.
|Postfix / Prefix||++, --|
|Unary||+, -, !, ~|
|Multiplicative||*, /, %|
|Relational||<, <=, >, >=|
|Bitwise||&, |, ^|
|Assignment||=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=|
Following is the example of implementing operator precedence in the c# programming language.
If you observe the above example, we implemented operator precedence with multiple operators. The evaluation of expressions will be done based on operators' priority in the c# programming language.
When we execute the above c# program, we will get the result as shown below.
This is how we can implement operator precedence in the c# programming language based on our requirements.