C# Operators Precedence with Examples

  Total Views : 50
  Comments : 0
  Keywords : C# operator precedence to define set of rules in single expression with examples, C# precedence of operators evaluation based on priority of operators with examples

Here we will learn precedence of operators in c# with examples and how to use operators precedence in c# to define multiple operators in single expression with examples.

C# Operators Precedence

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#.


int i = 3 + 4 * 5;

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 *, /, %
Additive +, -
Shift <<, >>
Relational <, <=, >, >=
Equality ==, !=
Bitwise &, |, ^
Logical &&, ||
Conditional ?:
Assignment =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

C# Operator Precedence Example

Following is the example of implementing an operator precedence in c# programming language.


using System;


namespace Tutlane


    class Program


        static void Main(string[] args)


            int x = 20, y = 5, z = 4;

            int result = x / y + z;

            Console.WriteLine("Result1: "+result);

            bool result2 = z <= y + x;

            Console.WriteLine("Result2: "+result2);

            Console.WriteLine("Press Any 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.

Output of C# Operators Precedence Example

When we execute above c# program, we will get the result like as shown below.


C# Operators Precedence Example Result


This is how we can implement an operator precedence in c# programming language based on our requirements. 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User