Operator
ศิริวรรณ สิงห์ลอ 650710095
Operators หรือ ตัวดำเนินการ เป็นพื้นฐานของภาษาโปรแกรมหลาย ๆ ภาษาโปรแกรม ดังนั้นการทำงานของภาษาโปรแกรม C# จะไม่สมบูรณ์ หากไม่มีการใช้ Operators
Operators ทำให้เราสามารถดำเนินการกับ Operands หรือ ตัวถูกดำเนินการ ได้
การจัดกลุ่มของ Operators ใน C#
การจัดกลุ่มตามฟังก์ชันการใช้งานที่แตกต่างกัน
Arithmetic Operators: ตัวดำเนินการทางคณิตศาสตร์
Relational Operators: ตัวดำเนินการแบบสัมพันธ์
Logical Operators: ตัวดำเนินการทางตรรกะ
Bitwise Operators: ตัวดำเนินการระดับบิต
Assignment Operators: ตัวดำเนินการกำหนดค่า
Conditional Operator: ตัวดำเนินการแบบเงื่อนไข
การจัดกลุ่มตามจำนวนตัวถูกดำเนินการ
Unary Operator: ตัวดำเนินการที่มีตัวถูกดำเนินการ 1 ตัว
Binary Operator: ตัวดำเนินการที่มีตัวถูกดำเนินการ 2 ตัว
Ternary Operator: ตัวดำเนินการที่มีตัวถูกดำเนินการ 3 ตัว
Arithmetic Operators - ตัวดำเนินการทางคณิตศาสตร์
Arithmetic Operators (Binary Operators)
ตัวดำเนินการทางคณิตศาสตร์ ที่มี ตัวถูกดำเนินการ 2 ตัว
Addition (+)
x+y
Subtraction (-)
x-y
Multiplication (*)
x*y
Division (/)
x/y
Modulus (%)
x%y
using System;
namespace Arithmetic
{
class GFG
{
// Main Function
static void Main(string[] args)
{
int result;
int x = 10, y = 5;
// Addition
result = (x + y);
Console.WriteLine("Addition Operator: " + result);
// Subtraction
result = (x - y);
Console.WriteLine("Subtraction Operator: " + result);
// Multiplication
result = (x * y);
Console.WriteLine("Multiplication Operator: "+ result);
// Division
result = (x / y);
Console.WriteLine("Division Operator: " + result);
// Modulo
result = (x % y);
Console.WriteLine("Modulo Operator: " + result);
}
}
}
Arithmetic Operators (Unary Operators)
ตัวดำเนินการทางคณิตศาสตร์ ที่มี ตัวถูกดำเนินการ 1 ตัว
Increment (++)
++x (pre-increment operator)
x++ (post-increment operator)
Decrement (--)
--x (pre-decrement operator)
x-- (post-decrement operator)
using System;
namespace Arithmetic {
class GFG {
// Main Function
static void Main(string[] args)
{
int a = 10, res;
// post-increment example:
// res is assigned 10 only,
// a is not updated yet
res = a++;
//a becomes 11 now
Console.WriteLine("a is {0} and res is {1}", a, res);
// post-decrement example:
// res is assigned 11 only, a is not updated yet
res = a--;
//a becomes 10 now
Console.WriteLine("a is {0} and res is {1}", a, res);
// pre-increment example:
// res is assigned 11 now since a
// is updated here itself
res = ++a;
// a and res have same values = 11
Console.WriteLine("a is {0} and res is {1}", a, res);
// pre-decrement example:
// res is assigned 10 only since
// a is updated here itself
res = --a;
// a and res have same values = 10
Console.WriteLine("a is {0} and res is {1}",a, res);
}
}
}
Relational Operators - ตัวดำเนินการแบบสัมพันธ์
ใช้เปรียบเทียบค่า 2 ค่า โดยจะคืนค่าเป็น จริง หรือ เท็จ (Boolean)
Equal To (==)
5==5 (return true)
Not Equal To (!=)
5!=5 (return false)
Greater Than (>)
6>5 (return true)
Less Than (<)
6<5 (return false)
Greater Than Equal To (>=)
5>=5 (return true)
Less Than Equal To (<=)
5<=5 (return true)
using System;
namespace Relational {
class GFG {
// Main Function
static void Main(string[] args)
{
bool result;
int x = 5, y = 10;
// Equal to Operator
result = (x == y);
Console.WriteLine("Equal to Operator: " + result);
// Greater than Operator
result = (x > y);
Console.WriteLine("Greater than Operator: " + result);
// Less than Operator
result = (x < y);
Console.WriteLine("Less than Operator: " + result);
// Greater than Equal to Operator
result = (x >= y);
Console.WriteLine("Greater than or Equal to: "+ result);
// Less than Equal to Operator
result = (x <= y);
Console.WriteLine("Lesser than or Equal to: "+ result);
// Not Equal To Operator
result = (x != y);
Console.WriteLine("Not Equal to Operator: " + result);
}
}
}
Logical Operators - ตัวดำเนินการทางตรรกะ
ใช้เชื่อมโยงสอง หรือมากกว่าสองเงื่อนไข ในการตัดสินใจว่าผลลัพธ์จะคืนค่าเป็น true หรือ false
Logical AND (&&)
a && b (returns true เมื่อทั้ง a และ b เป็นจริง)
Logical OR (||)
a||b (returns true เมื่อ a หรือ b เป็นจริง)
Logical NOT (!)
!a (returns true ถ้า a เป็นเท็จ)
using System;
namespace Logical {
class GFG {
// Main Function
static void Main(string[] args)
{
bool a = true,b = false, result;
// AND operator
result = a && b;
Console.WriteLine("AND Operator: " + result);
// OR operator
result = a || b;
Console.WriteLine("OR Operator: " + result);
// NOT operator
result = !a;
Console.WriteLine("NOT Operator: " + result);
}
}
}
Bitwise Operators - ตัวดำเนินการระดับบิต
ใน C# มีตัวดำเนินการระดับบิต 6 ตัว
bitwise AND (&)
A = 60, B = 13 จะได้เลขฐานสองเป็น
A = 0011 1100, B = 0000 1101
A&B = 0000 1100 (12)
bitwise OR (|)
A = 60, B = 13 จะได้เลขฐานสองเป็น
A = 0011 1100, B = 0000 1101
A|B = 0011 1101 (61)
bitwise XOR (^)
A = 60, B = 13 จะได้เลขฐานสองเป็น
A = 0011 1100, B = 0000 1101
A^B = 0011 0001 (49)
bitwise Complement (~)
A = 60 จะได้เลขฐานสองเป็น 0011 1100
เปลี่ยนจาก 0 เป็น 1 และ 1 เป็น 0
จะได้ ~A = 1100 0011 (-61)
left shift (<<)
A = 60 จะได้เลขฐานสองเป็น 0011 1100
60<<2 คือ การเลื่อนบิตไปทางซ้าย 2 บิต
จะได้ 1111 0000 (240)
right shift (>>)
A = 60 จะได้เลขฐานสองเป็น 0011 1100
60>>2 คือ การเลื่อนบิตไปทางขวา 2 บิต
จะได้ 0000 1111 (15)
using System;
namespace Bitwise {
class GFG {
// Main Function
static void Main(string[] args)
{
int x = 5, y = 10, result;
// Bitwise AND Operator
result = x & y;
Console.WriteLine("Bitwise AND: " + result);
// Bitwise OR Operator
result = x | y;
Console.WriteLine("Bitwise OR: " + result);
// Bitwise XOR Operator
result = x ^ y;
Console.WriteLine("Bitwise XOR: " + result);
// Bitwise Complement Operator
result = ~x;
Console.WriteLine("Bitwise Complement: " + result);
// Bitwise LEFT SHIFT Operator
result = x << 2;
Console.WriteLine("Bitwise Left Shift: " + result);
// Bitwise RIGHT SHIFT Operator
result = x >> 2;
Console.WriteLine("Bitwise Right Shift: " + result);
}
}
}
Assignment Operators - ตัวดำเนินการกำหนดค่า
ใช้กำหนดค่าให้กับตัวแปร โดยฝั่งซ้ายเป็นตัวแปร ส่วนฝั่งขวาเป็นค่า
Simple Assignment (=)
a = 10;
b = 20;
ch = 'y';
Add Assignment (+=)
// a += b (a = a + b)
a = 5
a += 6
a = 5 + 6 = 11
Subtract Assignment (-=)
// a -= b (a = a - b)
a = 8
a += 6
a = 8 - 6 = 2
Multiply Assignment (*=)
// a *= b (a = a * b)
a = 5
a *= 6
a = 5 * 6 = 30
Division Assignment (/=)
// a /= b (a = a / b)
a = 6
a /= 2
a = 6 / 2 = 3
Modulus Assignment (%=)
// a %= b (a = a % b)
a = 6
a %= 2
a = 6 % 2 = 0
Left Shift Assignment (<<=)
// a <<= 2 (a = a << 2)
a = 6
a <<= 2
a = 6 << 2
6 จะได้เลขฐานสองเป็น 0110
เลื่อนบิตไปทางซ้าย 2 บิต จะได้ 0001 1000 (24)
Right Shift Assignment (>>=)
// a >>= 2 (a = a >> 2)
a = 6
a >>= 2
a = 6 >> 2
6 จะได้เลขฐานสองเป็น 0110
เลื่อนบิตไปทางขวา 2 บิต จะได้ 0001 (1)
Bitwise AND Assignment (&=)
// a &= 2 (a = a & 2)
a = 6
a &= 2
a = 6 & 2
6 จะได้เลขฐานสองเป็น 0110
2 จะได้เลขฐานสองเป็น 0010
a &= 2 จะได้ 0010 (2)
Bitwise Exclusive OR (^=)
// a ^= 2 (a = a ^ 2)
a = 6
a ^= 2
a = 6 ^ 2
6 จะได้เลขฐานสองเป็น 0110
2 จะได้เลขฐานสองเป็น 0010
a ^= 2 จะได้ 0100 (4)
Bitwise Inclusive OR (|=)
// a |= 2 (a = a | 2)
a = 6
a |= 2
a = 6 | 2
6 จะได้เลขฐานสองเป็น 0110
2 จะได้เลขฐานสองเป็น 0010
a |= 2 จะได้ 0110 (6)
using System;
namespace Assignment {
class GFG {
// Main Function
static void Main(string[] args)
{
// initialize variable x
// using Simple Assignment
// Operator "="
int x = 15;
// it means x = x + 10
x += 10;
Console.WriteLine("Add Assignment Operator: " + x);
// initialize variable x again
x = 20;
// it means x = x - 5
x -= 5;
Console.WriteLine("Subtract Assignment Operator: " + x);
// initialize variable x again
x = 15;
// it means x = x * 5
x *= 5;
Console.WriteLine("Multiply Assignment Operator: " + x);
// initialize variable x again
x = 25;
// it means x = x / 5
x /= 5;
Console.WriteLine("Division Assignment Operator: " + x);
// initialize variable x again
x = 25;
// it means x = x % 5
x %= 5;
Console.WriteLine("Modulo Assignment Operator: " + x);
// initialize variable x again
x = 8;
// it means x = x << 2
x <<= 2;
Console.WriteLine("Left Shift Assignment Operator: " + x);
// initialize variable x again
x = 8;
// it means x = x >> 2
x >>= 2;
Console.WriteLine("Right Shift Assignment Operator: " + x);
// initialize variable x again
x = 12;
// it means x = x >> 4
x &= 4;
Console.WriteLine("Bitwise AND Assignment Operator: " + x);
// initialize variable x again
x = 12;
// it means x = x >> 4
x ^= 4;
Console.WriteLine("Bitwise Exclusive OR Assignment Operator: " + x);
// initialize variable x again
x = 12;
// it means x = x >> 4
x |= 4;
Console.WriteLine("Bitwise Inclusive OR Assignment Operator: " + x);
}
}
}
Conditional Operator - ตัวดำเนินการแบบเงื่อนไข
ใช้ตรวจสอบเงื่อนไขเพื่อการตัดสินใจในการทำงาน อยู่ในกลุ่ม Ternary Operator (ตัวดำเนินการที่มีตัวถูกดำเนินการ 3 ตัว)
Syntax:
condition ? first_expression : second_expression;
condition ถ้าเงื่อนไขเป็น จริง จะทำการประมวลผลในคำสั่ง first_expression และจะได้ผลลัพธ์จากการประมวลผลในคำสั่ง first_expression แต่ถ้าเงื่อนไขเป็น เท็จ จะทำการประมวลผลในคำสั่ง second_expression และจะได้ผลลัพธ์จากการประมวลผลในคำสั่ง second_expression
using System;
namespace Conditional {
class GFG {
// Main Function
static void Main(string[] args)
{
int x = 5, y = 10, result;
// To find which value is greater
// Using Conditional Operator
result = x > y ? x : y;
// To display the result
Console.WriteLine("Result: " + result);
// To find which value is greater
// Using Conditional Operator
result = x < y ? x : y;
// To display the result
Console.WriteLine("Result: " + result);
}
}
}
ลําดับความสําคัญ และการเรียงของตัวดําเนินการในภาษา C#
Unary
+, -, !, ~, ++, --, (type)* ,& ,sizeof
ขวา ไป ซ้าย
Additive
+, -
ซ้าย ไป ขวา
Multiplicative
%, /, *
ซ้าย ไป ขวา
Relational
<, >, <=, >=
ซ้าย ไป ขวา
Shift
<<, >>
ซ้าย ไป ขวา
Equality
==, !=
ขวา ไป ซ้าย
Logical AND
&
ซ้าย ไป ขวา
Logical OR
|
ซ้าย ไป ขวา
Logical XOR
^
ซ้าย ไป ขวา
Conditional OR
||
ซ้าย ไป ขวา
Conditional AND
&&
ซ้าย ไป ขวา
Null Coalescing
??
ซ้าย ไป ขวา
Ternary
?:
ขวา ไป ซ้าย
Assignment
=, *=, /=, %=, +=, - =, <<=, >>=, &=, ^=, |=, =>
ขวา ไป ซ้าย
เปรียบเทียบกับภาษา C# กับภาษา Java / C / Python
Arithmetic Operators
+, -, *, /, %, ++, --
+, -, *, /, %, ++, --
+, -, *, /, %, ++, --
+, -, *, /, %, **, //
Relational Operators
==, !=, >, <, >=, <=
==, !=, >, <, >=, <=
==, !=, >, <, >=, <=
==, !=, >, <, >=, <=
Logical Operators
&&, ||, !
&&, ||, !
&&, ||, !
and, or, not
Bitwise Operators
&, |, ^, ~, <<, >>
&, |, ^, ~
&, |, ^, ~, <<, >>
&, |, ^, ~, <<, >>
Assignment Operators
=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=
=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=
=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=
=, +=, -=, *=, /=, %=, //=, **=, &=, |=, ^=, <<=, >>=, :=
Conditional Operator
condition ? first_expression : second_expression;
ใช้ if-else
ใช้ if-else
ใช้ if-else
คลิปนำเสนอ
Presentation (slides)
References
Keshav_786 และ Mithun Kumar. (2567). C# | Operators. จากhttps://www.geeksforgeeks.org/c-sharp-operators/
tutorialspoint. (2567). C - Operators. จาก https://www.tutorialspoint.com/cprogramming/c_operators.htm
tutorialspoint. (2567). C - Bitwise Operators in C จาก https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm
w3schools. (2567). C# Operators. จาก https://www.w3schools.com/cs/cs_operators.php
w3schools. (2567). C# Assignment Operators. จาก https://www.w3schools.com/cs/cs_operators_assignment.php
w3schools. (2567). C# Comparison Operators. จาก https://www.w3schools.com/cs/cs_operators_comparison.php
w3schools. (2567). C# Logical Operators. จาก https://www.w3schools.com/cs/cs_operators_logical.php
javatpoint. (2567). C# operators. จาก https://www.javatpoint.com/csharp-operators
learn.microsoft. (2566). C# operators and expressions. จากhttps://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/
learn.microsoft. (2566). Arithmetic operators (C# reference). จากhttps://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators
learn.microsoft. (2566). Boolean logical operators - AND, OR, NOT, XOR. จากhttps://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators
learn.microsoft. (2566). Bitwise and shift operators (C# reference). จาก https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators
learn.microsoft. (2566). Equality operators - test if two objects are equal or not. จาก https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-operators
learn.microsoft. (2566). Comparison operators (C# reference). จาก https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/comparison-operators
w3schools. (2567). C Operators. จาก https://www.w3schools.com/c/c_operators.php
w3schools. (2567). C If ... Else. จาก https://www.w3schools.com/c/c_conditions.php
w3schools. (2567). Python Operators. จาก https://www.w3schools.com/python/python_operators.asp
w3schools. (2567). Python If ... Else. จาก https://www.w3schools.com/python/python_conditions.asp
w3schools. (2567). Java Operators. จาก https://www.w3schools.com/java/java_operators.asp
w3schools. (2567). Java If ... Else. จาก https://www.w3schools.com/java/java_conditions.asp
kartik. (2567). Bitwise Operators in Java. จาก https://www.geeksforgeeks.org/bitwise-operators-in-java/
Last updated