Increment Operators
มี 2 แบบ: Pre-increment (++a) และ Post-increment (a++)
ตัวอย่าง Increment
using System;
class Demo
{
static void Main()
{
int a = 250;
Console.WriteLine(a); // 250
a++; Console.WriteLine(a); // 251
++a; Console.WriteLine(a); // 252
int b = 0;
b = a++; Console.WriteLine(b); // 252 (Post: ให้ค่าก่อนเพิ่ม)
Console.WriteLine(a); // 253
b = ++a; Console.WriteLine(b); // 254 (Pre: เพิ่มก่อนให้ค่า)
Console.WriteLine(a); // 254
}
}
Decrement Operators
Pre-decrement (--a) และ Post-decrement (a--)
ตัวอย่าง Decrement
using System;
class Demo
{
static void Main()
{
int a = 250;
Console.WriteLine(a);
a--; Console.WriteLine(a);
--a; Console.WriteLine(a);
int b = 0;
b = a--; Console.WriteLine(b);
Console.WriteLine(a);
b = --a; Console.WriteLine(b);
Console.WriteLine(a);
}
}