隱式轉換, 只要容得下就行:
using System;
class MyClass
{
static void Main()
{
byte n = byte.MaxValue;
short n1 = n;
int n2 = n;
long n3 = n;
Console.WriteLine("{0},{1},{2}", n1, n2, n3); //255,255,255
Console.ReadKey();
}
}
顯示轉換, 可能會因溢出而丟失數據:
using System;
class MyClass
{
static void Main()
{
ulong n = ulong.MaxValue;
byte n1 = (byte)n;
ushort n2 = (ushort)n;
uint n3 = (uint)n;
Console.WriteLine("{0},{1},{2}", n1, n2, n3); //255,65535,4294967295
Console.ReadKey();
}
}