ข้อควรระวังในการใช้งาน Nullable Type in CSharp by joeskb
31
Dec0
Dec0
การใช้งานที่สะดวกขึ้นของ Nullable Type ใน CSharp ซึ่งถ้าใช้ไม่ถูกมันก็จะพาให้ Code เพี้ยนไปได้นะครับ
ขึ้นชื่อว่า Nullable ที่วิธีการเช็คค่าที่ดี ก็ไม่ควรไปใช้ == null หรือ != null ตัวอย่างเช่น
int? element;
element = foo(bar);
if(element.HasValue){
Debug.Log(element.Value);
}else{
Debug.Log("There is no Value");
}
วิธีใช้งานที่ดีคือ ให้ใช้ HasValue แทนการเช็คค่า Null
และถ้าอยู่ใน Condition HasValue ก็อย่าลืมใช้ Value ในการเรียกใช้ตัวแปรนะครับ
Happy New Year
Enjoy this article?
Consider subscribing to our RSS feed!
ไม่มีความเห็น
ยังไม่มีความเห็น