What is the operator precedence in C#?


An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

Operator precedence determines the grouping of terms in an expression. This affects evaluation of an expression. Certain operators have higher precedence than others do; for example, the multiplication operator has higher precedence than the addition operator.

The following is an example showing operator precedence −

Example

 Live Demo

using System;
namespace OperatorsAppl {
   class Program {
      static void Main(string[] args) {
         int a = 20;
         int b = 10;
         int c = 15;
         int d = 5;
         int e;
         e = (a + b) * c / d; // ( 30 * 15 ) / 5
         Console.WriteLine("Value of (a + b) * c / d is : {0}", e);

         e = ((a + b) * c) / d; // (30 * 15 ) / 5
         Console.WriteLine("Value of ((a + b) * c) / d is : {0}", e);

         e = (a + b) * (c / d); // (30) * (15/5) {0}", e);

         e = a + (b * c) / d; // 20 + (150/5) {0}", e);
         Console.ReadLine();
      }
   }
}

Output

Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90

Updated on: 20-Jun-2020

241 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements