การแปลง type ใน C# มีหลายแบบ
- Implicit conversion - แปลงแบบอัติโนมัติโดย compiler
- Explicit conversion (cast) - แปลงโดยใช้ keyword
as
หรือ ใช้ operator cast - User-define conversion - เขียน method สำหรับแปลง type ขึ้นเอง
- Helper class - ใช้ helper ที่มีอยู่แล้ว เช่น คลาส
Convert
Explicit conversion
ใช้ cast
1 | object x = ...; |
Cast มักใช้คู่กับ is
เพื่อทำ type checking สำหรับกัน error InvalidCastException
ในกรณีที่ CLR ไม่สามารถแปลง object ให้เป็น type ที่ต้องการได้
ใช้ as
1 | var str = x as string; |
การแปลง type โดยใช้ as
จะช่วยลดขั้นตอน type checking และไม่มีทาง error เนื่องจาก operator as
จะคืนค่า null
ในกรณีที่ไม่สามารถแปลง type จะต่างจาก cast ที่จะเกิด error InvalidCastException
ข้อจำกัดของ as
คือใช้ได้กับ type ที่เป็น Nullable
เท่านั้น
ใช้ as
ใน for
1 | for (var str = x as string; str != null; str = null) { |
เราสามารถใช้ as
ว่างไว้ใน expression for เพื่อกันตังแปร str
ให้อยู่ใน scope วิธีนี้เป็น programming trick ที่ทำให้โค้ดอ่านยาก จึงไม่นิยมใช้
Performance
การ convert type มีผลกับประสิทธิภาพของโปรแกรม ถ้าไม่สามารถหลีกเลี่ยงได้ ก็ควรทำให้น้อยที่สุด การใช้ as
แทน cast จะช่วยลดจำนวน conversion ได้หนึ่งครั้ง เนื่องจากการทำ type checking ด้วย is
จะเกิด type conversion เช่นกัน
อย่างไรก็ตามการใช้ as กับข้อมูลประเภท Nullable
ของ value type เช่น int?
จะ drop performance ของโปรแกรมมากกว่าการใช้ is
+ cast อย่างมีนัยยสำคัญ เนื่องจากการแปลง value type ให้เป็น Nullable
จะมี operator อื่นเพิ่มเข้ามา
1 | class Test { |
จากตัวอย่าง FindSumWithCast
การ cast แบบปกติ สามารถ unbox
object ให้เป็น type int
ได้โดยตรง ส่วน FindWithAs
จะต้อง unbox
ให้เป็น Nullable int
หรือ int?
ซึ่งจะมี internal operation เกิดขึ้นในฟังก์ชั่น JIT_Unbox_Nullable
ของ JIT
1 | Cast: 10000000 : 74 |
ผลการรัน ในกรณีที่ใช้ cast ให้ประสิทธิภาพที่ดีกว่าการใช้ as
ถึงแม้จะมี type conversion เกิดขึ้นสองครั้งก็ตาม
Links