Boxing vs Unboxing
สุชานัน คดดี 650710098
Boxing และ Unboxingเป็นกระบวนการแปลงข้อมูลจากvalue type เป็นreference type ซึ่งเป็นกระบวนการที่สำคัญในภาษาC# โดยที่กระบวนการนั้นจะแยกกันอย่างชัดเจน ประกอบไปด้วยค่าสามค่า นั้นก็คือ Value Types (int, char, etc), Reference Types (object) และPointer Types. โดยแบ่งเป็นกระบวนการBoxing และ Unbox
Example
using System;
public class Boxing{
static public void Main()
{
int number = 2024;
char letter = 'C';
object o = number ;
object l = letter ;
number = 2000;
Console.WriteLine("Value type of val is {0}", number);
Console.WriteLine("Object type of letter is {0}", l);
Console.WriteLine("Object type of number is {0}", o);
}
}
จากโค้ดด้านบนเป็นกระบวนการBoxing โดยจะแปลงletter ในบรรทัด7 ซึ่งเป็น Value Types (char) และ number ในบรรทัดที่6 ซึ่งเป็น Value Types (int) ให้เป็นreference type number --> o และ letter --> l จากนั้นในบรรทัดที่12จะสาธิตการเปลี่ยนค่าnumber เป็น 2000 จะเห็นได้ว่า output ของค่าที่เก็บเอาไว้แล้วจะไม่เปลี่ยน บรรทัดที่ทำการBoxingคือ 9และ10
using System;
public class Unbox{
static public void Main()
{
int number = 2024;
char letter = 'C';
object o = number ;
object l = letter ;
int x = (int)o;
char xx = (char)l;
Console.WriteLine("Value type of val is {0}", number);
Console.WriteLine("Object type of letter is {0}", l);
Console.WriteLine("Object type of number is {0}", o);
}
}
จากโค้ด เป็นกระบวนการUnboxingโดยเพิ่มบรรทัดที่12 และ บรรทัดที่13 เป็นการtypecastจากobjectที่ทำการBoxingเอาไว้ให้เป็นvalue typesดังเดิม (letter --> l -->xx)(number --> o --> x)แสดงผลเป็นได้ดั่งoutputข้างต้น
ความแตกต่างของBoxing และ Unboxing
ขอแสดงความแตกต่างในรูปแบบตารางเพื่อให้เห็นความชัดเจนได้ดังนี้
value types --> reference type
referece types --> value types
เป็นการแปลงโดยนัย
ประกาศtypecastingอย่างชัดเจน
เป็นการcopyข้อมูลใส่ไว้ในheap memoryจากนั้นจึงตั้งให้ตัวแปร ของobject ชี้ไปหาข้อมูลที่copyไว้
เป็นการcopyข้อมูลจากheap memoryแล้วทำการtypecastข้อมูลอีกทีก่อน
เปรียบเทียบBoxing และUnboxingในภาษาอื่นๆ
Boxing และ Unboxingเป็นconcepที่พบได้ในภาษาC# เพราะฉะนั้นการเปรียบเทียบกับภาษาอื่นๆนั้นจึงต้องเปรียบเทียบกับภาษาที่มีconceptคล้ายๆกัน แต่ก็จะขอยกตัวอย่างภาษาที่ไม่มีกลไกหรือคอนเซ็ปต์นี้ด้วย
ภาษา Java: Java มีกลไก autoboxing และ auto-unboxing ซึ่งคล้ายคลึงกับ C# แต่มีความแตกต่างในรายละเอียดบางประการ
ภาษา Python: Python มีการแปลงประเภทข้อมูลแบบอัตโนมัติ (type coercion) ซึ่งมีความยืดหยุ่นสูง แต่ไม่มีกลไกที่ชัดเจนเหมือน boxing และ unboxing ใน C#
ภาษา C และ C++: ภาษาเหล่านี้ให้การควบคุมหน่วยความจำที่ละเอียดมากขึ้น โปรแกรมเมอร์ต้องจัดการกับการจัดสรรและปล่อยหน่วยความจำเอง การแปลงระหว่างประเภทข้อมูลมักทำได้โดยตรง โดยไม่มีกลไกการ boxing หรือ unboxing ที่ชัดเจน
ตารางเปรียบเทียบ
C#
มี
Garbage collection
Java
auto boxing/auto unboxing
Garbage collection
Python
Type coercion
Garbage collection
C/C++
ไม่มี
โปรแกรมเมอร์จัดการด้วยตัวเอง
using System;
public class Program
{
public static void Main()
{
int number = 2024;
object obj = number;
Console.WriteLine(obj);
int originalNumber = (int)obj;
Console.WriteLine("Original number: " + originalNumber);
}
}
Video Presentation
Slide Presentation
References:
GeeksforGeeks. "Difference between Boxing and Unboxing in C#." GeeksforGeeks, n.d., https://www.geeksforgeeks.org/difference-between-boxing-and-unboxing-in-c/.
GeeksforGeeks. "Type Conversion in Python." GeeksforGeeks, n.d., https://www.geeksforgeeks.org/type-conversion-in-python/.
Microsoft Learn. "Boxing and Unboxing - C#." Microsoft Learn, n.d., https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/boxing-and-unboxing.
saladpuk. (n.d.). Boxing and Unboxing - C#. saludpuk. Retrieved October 12, 2024, from https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/boxing-and-unboxing
javatpoint. (n.d.). "Autoboxing and Unboxing in Java." javatpoint.com .Retrieved October 12, 2024, from https://www.javatpoint.com/autoboxing-and-unboxing.
Last updated