In c#, Operators Precedence is used to define a multiple operators in single expression and the evaluation of expression can be happened based on the priority of operators.
For example, the multiplication operator (*) is having a higher precedence than the addition operator (+). So if we use both multiplication (*) and addition (+) operators in 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 above statement, first the multiplication part (4 * 5) will be evaluated. After that the addition part (3 + 12) will be executed and i value will become a 23.
As said earlier, multiplication (*) operator is having a higher precedence than addition (+) operator so first multiplication part will be executed.
Following table lists the different type of operators available in c# relational operators.
|Postfix / Prefix||++, --|
|Unary||+, -, !, ~|
|Multiplicative||*, /, %|
|Relational||<, <=, >, >=|
|Bitwise||&, |, ^|
|Assignment||=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=|
Following is the example of implementing an operator precedence in c# programming language.
static void Main(string args)
int x = 20, y = 5, z = 4;
int result = x / y + z;
bool result2 = z <= y + x;
Console.WriteLine("Press Enter Key to Exit..");
If you observe above example, we are implemented an operator precedence with multiple operators and the expressions evaluation will be done based on the priority of operators in c# programming language.
When we execute above c# program, we will get the result like as shown below.
This is how we can implement an operator precedence in c# programming language based on our requirements.