C# - 算术运算符



下表显示了 C# 支持的所有算术运算符。假设变量A 为 10,变量B 为 20,则:

运算符 描述 示例
+ 将两个操作数相加 A + B = 30
- 从第一个操作数中减去第二个操作数 A - B = -10
* 将两个操作数相乘 A * B = 200
/ 将分子除以分母 B / A = 2
% 取模运算符,返回整数除法后的余数 B % A = 0
++ 自增运算符将整数的值增加一 A++ = 11
-- 自减运算符将整数的值减少一 A-- = 9

示例

以下示例演示了 C# 中可用的所有算术运算符:

using System;

namespace OperatorsAppl {
   class Program { 
      static void Main(string[] args) { 
         int a = 21;
         int b = 10;
         int c;

         c = a + b;
         Console.WriteLine("Line 1 - Value of c is {0}", c);
         
         c = a - b;
         Console.WriteLine("Line 2 - Value of c is {0}", c);
         
         c = a * b;
         Console.WriteLine("Line 3 - Value of c is {0}", c);
         
         c = a / b;
         Console.WriteLine("Line 4 - Value of c is {0}", c);
         
         c = a % b;
         Console.WriteLine("Line 5 - Value of c is {0}", c);
         
         c = a++;
         Console.WriteLine("Line 6 - Value of c is {0}", c);
         
         c = a--;
         Console.WriteLine("Line 7 - Value of c is {0}", c);
         Console.ReadLine();
      }
   }
}

编译并执行上述代码后,将产生以下结果:

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 21
Line 7 - Value of c is 22
csharp_operators.htm
广告