Toán tử tăng (Increment)
Tăng giá trị: tiền tố (++a) và hậu tố (a++).
Ví dụ
using System;
class Demo {
static void Main() {
int a = 250;
Console.WriteLine(a); // 250
a++; // hậu tố
Console.WriteLine(a); // 251
++a; // tiền tố
Console.WriteLine(a); // 252
int b = 0;
b = a++; // gán rồi tăng
Console.WriteLine(b); // 252
Console.WriteLine(a); // 253
b = ++a; // tăng rồi gán
Console.WriteLine(b); // 254
Console.WriteLine(a); // 254
}
}
Toán tử giảm (Decrement)
Giảm giá trị: tiền tố (--a) và hậu tố (a--).
Ví dụ
using System;
class Demo {
static void Main() {
int a = 250;
Console.WriteLine(a); // 250
a--; // hậu tố
Console.WriteLine(a); // 249
--a; // tiền tố
Console.WriteLine(a); // 248
int b = 0;
b = a--; // gán rồi giảm
Console.WriteLine(b); // 248
Console.WriteLine(a); // 247
b = --a; // giảm rồi gán
Console.WriteLine(b); // 246
Console.WriteLine(a); // 246
}
}