ภาพรวม
byte (8-bit unsigned integer) สามารถแปลงเป็น decimal แบบโดยนัย (implicit) ได้โดยตรง เพราะ decimal มีขอบเขตครอบคลุม byte หมด
รูปแบบการใช้งาน
byte val = 16; decimal dec = val; // implicit conversion
ตัวอย่างโค้ดเต็ม
using System;
public class Demo
{
public static void Main()
{
byte val = 16;
decimal dec = val; // implicit
Console.WriteLine("Decimal = " + dec);
}
}
ผลลัพธ์
Decimal = 16
เหตุผลที่แปลงโดยนัยได้
byteขอบเขต 0 ถึง 255decimalขอบเขตประมาณ ±7.9 × 10²⁸ ความแม่นยำ 28-29 หลักนัยสำคัญ- ไม่มีความเสี่ยงล้น (overflow) หรือสูญเสียความแม่นยำ
- คอมไพเลอร์อนุญาตให้กำหนดค่าโดยตรงไม่ต้อง cast