ออเปอเรเตอร์คือสัญลักษณ์ที่บอกคอมไพเลอร์ให้ทำการจัดการทางคณิตศาสตร์หรือตรรกะเฉพาะเจาะจง
ลำดับความสำคัญของออเปอเรเตอร์ (Operator Precedence) กำหนดการจัดกลุ่มของพจน์ในนิพจน์ ซึ่งส่งผลต่อลำดับการประเมินค่า ออเปอเรเตอร์บางตัวมีลำดับความสำคัญสูงกว่าอื่นๆ เช่น ออเปอเรเตอร์คูณมีลำดับสูงกว่าบวก
ตัวอย่าง
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 = 90
Console.WriteLine("Value of (a + b) * c / d is : {0}", e);
e = ((a + b) * c) / d; // (30 * 15) / 5 = 90
Console.WriteLine("Value of ((a + b) * c) / d is : {0}", e);
e = (a + b) * (c / d); // 30 * (15/5) = 90
Console.WriteLine("Value of (a + b) * (c / d) is : {0}", e);
e = a + (b * c) / d; // 20 + (150/5) = 50
Console.WriteLine("Value of a + (b * c) / d is : {0}", e);
Console.ReadLine();
}
}
}
ผลลัพธ์
Value of (a + b) * c / d is : 90 Value of ((a + b) * c) / d is : 90 Value of (a + b) * (c / d) is : 90 Value of a + (b * c) / d is : 50