What is the operator precedence in C#?

CsharpProgrammingServer Side Programming

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
raja
Published on 07-Aug-2018 14:34:57
Advertisements