C# 中的运算符优先级是什么?

csharpprogrammingserver side programming更新于 2025/10/15 14:37:17

运算符是指示编译器执行特定数学或逻辑运算的符号。

运算符优先级决定了表达式中项的分组。这会影响表达式的求值。某些运算符的优先级高于其他运算符;例如,乘法运算符的优先级高于加法运算符。

以下示例展示了运算符优先级 −

示例

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();
      }
   }
}

输出

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

相关文章