วันจันทร์ที่ 22 มีนาคม พ.ศ. 2553

.NET Stong type

อันนี้คือ concept ตั้งแต่ .Net 2.0 แล้วนะครับ คือ object ที่สาำมารถรู้ได้ทันทีว่าเป็น object ชนิดไหนตั้งแต่ตอน compile time เพื่อลดการ boxing และ unboxing

ยกตัวอย่างเช่น

กรณีที่ไม่เป็น strong type
ArrayList ar = new ArrayList(); // คุณสามารถ add อะไรก็ได้แข้า arraylist ตัวนี้
ar.Add("CCC"); // adding string
ar.Add(new object()); // adding object

กรณีที่เป็น stong type
List<string> ar2 = new List<string>();
ar2.Add("BBB"); // คุณจะ add ได้เฉพาะ string เท่านั้น เพราะว่าคุณระบุว่าจะรับ type แค่ string

กรณีของ strong type ถ้าคุณ add object ที่ไม่ใช่ string จะ compile ไม่ผ่าน และการทำงานแบบ strong type ก็จะทำงานเร็วกว่าด้วยครับ

ไม่มีความคิดเห็น:

แสดงความคิดเห็น