Type Conversion ใน C# แบ่ง 2 แบบ:
1. Implicit Type Conversion
Compiler ทำให้อัตโนมัติ Type-safe (เล็ก->ใหญ่, Derived->Base)
2. Explicit Type Conversion
ผู้ใช้ทำเองด้วย Cast Operator ต้องการ Cast Operator
ตัวอย่าง Explicit: Double -> Int
using System;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
double d = 8745.97;
int x = (int)d; // Explicit cast
Console.WriteLine(x); // 8745 (ตัดทศนิยม)
Console.ReadKey();
}
}
}
Output
8745