ตัวดำเนินการคือสัญลักษณ์ที่บอกคอมไพล์อร์ให้ดำเนินการทางคณิตศาสตร์หรือตรรกะเฉพาะเจาะจง
ตารางตัวดำเนินการเลขคณิต
| ตัวดำเนินการ | คำอธิบาย | ตัวอย่าง |
|---|---|---|
| + | บวกสองตัวถูกดำเนินการ | A + B = 30 |
| - | ลบตัวถูกดำเนินการตัวที่สองจากตัวแรก | A - B = -10 |
| * | คูณทั้งสองตัวถูกดำเนินการ | A * B = 200 |
| / | หารตัวเศษด้วยตัวส่วน | B / A = 2 |
| % | Modulus: เศษจากการหารจำนวนเต็ม | B % A = 0 |
| ++ | Increment: เพิ่มค่าจำนวนเต็มขึ้น 1 | A++ = 11 |
| -- | Decrement: ลดค่าจำนวนเต็มลง 1 | A-- = 9 |
ตัวอย่าง
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
int a = 99;
int b = 33;
int c;
c = a + b; Console.WriteLine("Value of c is {0}", c);
c = a - b; Console.WriteLine("Value of c is {0}", c);
c = a * b; Console.WriteLine("Value of c is {0}", c);
c = a / b; Console.WriteLine("Value of c is {0}", c);
c = a % b; Console.WriteLine("Value of c is {0}", c);
c = a++; Console.WriteLine("Value of c is {0}", c);
c = a--; Console.WriteLine("Value of c is {0}", c);
Console.ReadLine();
}
}
}
ผลลัพธ์
Value of c is 132 Value of c is 66 Value of c is 3267 Value of c is 3 Value of c is 0 Value of c is 99 Value of c is 100