Type System Unification in C# .NET
ณฐกฤต มากมิตร 650710075
ในภาษา C# นั้น ประเภทข้อมูล(Data Types) ทั้งหมดนั้นจะสืบทอดมาจาก Object Class หรือจะเรียกได้ว่าข้อมูลทุกประเภทถูกพิจารณาเป็น Object
ก่อนจะเข้าสู่เรื่องถัดไป ต้องขอเกริ่นสักนิดก่อนว่า ในภาษา C# มีประเภทข้อมูลหลักๆอยู่ 2 ประเภท และ เพื่อให้เข้าใจง่ายขึ้นสำหรับผู้ที่เคยเรียนหรือเขียนโปรแกรภาษาอื่นๆมาก่อน
จึงจะขอยกตัวอย่างจากภาษาอื่น ๆ ร่วมด้วยอีก 2 ภาษา คือภาษา Java และ Python ได้แก่
1. Value Type
เรียกได้ว่าเป็นตัวแปรพื้นฐาน เป็นประเภทข้อมูลที่เก็บค่าข้อมูลโดยตรงในหน่วยความจำ(Stack) หรืออย่างในภาษา Java จะเรียกว่า Primitive Data Type ตัวอย่างประเภทข้อมูลที่เป็น Value Type
byte
byte, signed byte
byte
int (0-255)
char
char, signed char, unsigned char
char
str
short
short, signed short, unsigned short
short
int
int
int, signed int, unsigned int
int
int
long
long, signed long, unsigned long
long
int ( float for large values)
float
float
float
float
double
double
double
float
decimal
decimal
BigDecimal
-
boolean
bool
boolean
bool
ตัวอย่างการประกาศตัวแปรประเภท Value Type
2. Reference Type
เป็นประเภทข้อมูลที่เก็บการอ้างอิง (pointer) ซึ่งข้อมูลนั้นจะถูกเก็บไว้ใน heap ตัวอย่างประเภทข้อมูลที่เป็น Reference Types
class
class
class
class
interface
interface
interface
-
delegate
delegate
-
-
array
array
array
list
string
string
String
str
enum
enum
enum
-
ตัวอย่างการประกาศตัวแปรประเภท Reference Type
จากที่เกริ่นนำประเภทของตัวแปรข้างต้นไปนั้น เราจะมาพูดถึงแนวคิดสำคัญใน C# ที่ช่วยให้การจัดการกับ
Value Types และ Reference Types ซึ่งก็คือการทำ Boxing และ Unboxing นั่นเอง
การทำ Boxing และ Unboxing
เป็นการทำเพื่อแปลงประเภทข้อมูลระหว่าง Value Type และ Reference Type
Boxing
เป็นการแปลงจาก Value Type (int, float และอื่นๆ) -> Reference Type (Object) เปรียบเหมือนกับ
การเก็บข้อมูลลงในกล่องที่ชื่อว่า Object
ภาษา C#
ภาษา Java
ในภาษา Java ไม่มีการทำ Boxing แต่จะมีสิ่งที่คล้ายกันคือการทำ Autoboxing โดยจะแปลงจาก
Value Type -> Wrapper Classes เช่น Integer, Double
ภาษา Python
ในภาษา Python ไม่มีการทำ Boxing เพราะทุกตัวถือว่าเป็น Object แต่สามารถทำให้คล้ายกันได้โดยการเก็บ
ข้อมูลลงใน List
Unboxing
เป็นการแปลงจาก Reference Type (Object) -> Value Type (int, float และอื่นๆ) เปรียบเหมือนกับ
นำสิ่งของหรือข้อมูลออกมาจากกล่องหรือก็คือ Object
ภาษา C#
ภาษา Java
ในภาษา Java ไม่มีการทำ Unboxing แต่จะมีสิ่งที่คล้ายกันคือการทำ Auto-unboxing โดยจะแปลงจาก
Wrapper Classes -> Value Type
ภาษา Python
ในภาษา Python ไม่มีการทำ Unboxing แต่สามารถทำให้คล้ายกันได้โดยการนำจ้อมูลใน List ออกมาใส่ไว้ในตัวแปรหนึ่ง
Slide Presentation
Video Presentation
Reference
harkiran78. (2019, May 06). Type System Unification in C# .NET: geeksforgeeks. Type System Unification in C# .NET
w3schools. (n.d.). C# Data Types: C# Data Types[w3schools]. Data Types: C#
tutorialspoint. (n.d.). C# - Data Types: C# Data Types[tutorialspoint]. C# - Data Types
javatpoint. (n.d.). C# Data Types: C# Data Types[javatpoint]. C# Data Types javapoint
Microsoft 2024. (2022, Sep 29). Value types (C# reference): Value types (C# reference)[Microsoft Learn]. Value types (C# reference) Microsoft Learn
TutorialsTeacher. (n.d.). Value Type and Reference Type: Value Type and Reference Type[tutorialsteacher]. TutorialTeacher Value Type and Reference Type
Microsoft 2024. (2021, Sep 15). Boxing and Unboxing (C# Programming Guide): Boxing and Unboxing (C# Programming Guide)[Microsoft Learn]. Boxing and Unboxing (C# Programming Guide) Microsoft Learn
geeksforgeeks. (2022, Apr 20). Autoboxing and Unboxing in Java: geeksforgeeks. Autoboxing and Unboxing in Java
Last updated