Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Unboxing ใน C# คืออะไร

Boxing เป็น Implicit Conversion จาก Value Type เป็น Reference Type (Object)

Unboxing เป็น Explicit Conversion กลับจาก Object เป็น Value Type เดิม

ตัวอย่าง:

int x = 30;
object obj = x;        // Boxing
int unboxInt = (int)obj; // Unboxing

ตัวอย่างกับ ArrayList:

int x = 5;
ArrayList arrList = new ArrayList();
arrList.Add(x);        // Boxing
int y = (int)arrList[0]; // Unboxing