眾所周知,像“int a = 10; short b = a“這樣的語句是無法編譯通過的,原因是cannot implicitly convert type 'int' to 'short'。而我寫上“short = 10”這樣的語句是沒有問題的,即沒有錯誤也沒有警告,這是為什麼呢,難道編譯器自動幫我加上強制類型轉換?為了揭開這些方面的謎題,我做了些測試,因此有了本文。
C#基本數字資料類型一共有11種,其中8種整數類型(byte, sbyte, short, ushort, int, uint, long, ulong),3種可帶小數類型(double, float, decimal).首先,我對8種整數類型做了如下測試:
class Program
{
public static byte _byte;
public static sbyte _sbyte;
public static short _short;
public static ushort _ushort;
public static int _int;
public static uint _uint;
public static long _long;
public static ulong _ulong;
public static byte _byte2;
public static short _short2;
public static uint _uint2;
public static long _long2;
static void Main(string[] args)
{
_byte = 20;
_sbyte = 20;
_short = 20;
_ushort = 20;
_int = 20;
_uint = 20;
_long = 20;
_ulong = 20;
_byte2 = (byte)20;
_short2 = (short)20;
_uint2 = 20U;
_long2 = 20L;
}
}
產生的IL代碼如下:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 89 (0x59)
.maxstack 1
IL_0000: nop
IL_0001: ldc.i4.s 20
IL_0003: stsfld uint8 ConsoleApplication1.Program::_byte
IL_0008: ldc.i4.s 20
IL_000a: stsfld int8 ConsoleApplication1.Program::_sbyte
IL_000f: ldc.i4.s 20
IL_0011: stsfld int16 ConsoleApplication1.Program::_short
IL_0016: ldc.i4.s 20
IL_0018: stsfld uint16 ConsoleApplication1.Program::_ushort
IL_001d: ldc.i4.s 20
IL_001f: stsfld int32 ConsoleApplication1.Program::_int
IL_0024: ldc.i4.s 20
IL_0026: stsfld uint32 ConsoleApplication1.Program::_uint
IL_002b: ldc.i4.s 20
IL_002d: conv.i8
IL_002e: stsfld int64 ConsoleApplication1.Program::_long
IL_0033: ldc.i4.s 20
IL_0035: conv.i8
IL_0036: stsfld uint64 ConsoleApplication1.Program::_ulong
IL_003b: ldc.i4.s 20
IL_003d: stsfld uint8 ConsoleApplication1.Program::_byte2
IL_0042: ldc.i4.s 20
IL_0044: stsfld int16 ConsoleApplication1.Program::_short2
IL_0049: ldc.i4.s 20
IL_004b: stsfld uint32 ConsoleApplication1.Program::_uint2
IL_0050: ldc.i4.s 20
IL_0052: conv.i8
IL_0053: stsfld int64 ConsoleApplication1.Program::_long2
IL_0058: ret
} // end of method Program::Main
我們發現,_byte = 20 與 _byte = (byte)20 產生的程式碼一模一樣,_long = 20 與 _long = 20L產生的程式碼也一模一樣。因此,結合一些其他測試(這裡省略) ,我們得出以下結論:
1、_byte = 20 與 _byte = (byte)20 的效益一樣。
2、_long = 20 與 _long = 20L 的效益一樣,20L中的“L”僅僅在編譯中起作用。
3、編譯器會自動檢測直接賦的值是否超過該類型所能表示的最大範圍. ( _byte = 256 會導致編譯出錯)
接下來我們對double,float,decimal進行測試:(注釋的行表示會導致編譯無法通過)
class Program
{
public static float _float;
public static double _double;
public static decimal _decimal;
static void Main(string[] args)
{
_float = 5;
//_float = 5.0;
_float = 5F;
//_float = 5M;
_double = 5;
_double = 5.0;
_double = 5F;
//_double = 5M;
_decimal = 5;
//_decimal = 5.0;
_decimal = 5.0M;
//_decimal = 5.0F;
}
}
IL代碼如下:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 91 (0x5b)
.maxstack 6
IL_0000: nop
IL_0001: ldc.r4 5.
IL_0006: stsfld float32 ConsoleApplication1.Program::_float
IL_000b: ldc.r4 5.
IL_0010: stsfld float32 ConsoleApplication1.Program::_float
IL_0015: ldc.r8 5.
IL_001e: stsfld float64 ConsoleApplication1.Program::_double
IL_0023: ldc.r8 5.
IL_002c: stsfld float64 ConsoleApplication1.Program::_double
IL_0031: ldc.r8 5.
IL_003a: stsfld float64 ConsoleApplication1.Program::_double
IL_003f: ldc.i4.5
IL_0040: newobj instance void [mscorlib]System.Decimal::.ctor(int32)
IL_0045: stsfld valuetype [mscorlib]System.Decimal ConsoleApplication1.Program::_decimal
IL_004a: ldc.i4.s 50
IL_004c: ldc.i4.0
IL_004d: ldc.i4.0
IL_004e: ldc.i4.0
IL_004f: ldc.i4.1
IL_0050: newobj instance void [mscorlib]System.Decimal::.ctor(int32,
int32,
int32,
bool,
uint8)
IL_0055: stsfld valuetype [mscorlib]System.Decimal ConsoleApplication1.Program::_decimal
IL_005a: ret
} // end of method Program::Main
結論:
1、跟整數類型不同,帶小數類型在直接賦數值時必須指定相應數實值型別 或 可由該預設數實值型別隱式轉化為該變數類型。
2、_float = 5 與 _float = 5F 的效率一樣。"F" 同樣只在編譯中起作用。
3、decimal類型的賦值跟其它類型有些不同,查decimal的構造方法發現,它有9個公有構造方法。
以上就是我對數實值型別的簡單分析,如有不足或錯誤,歡迎大家指出。
附:
L 表示 long
D 表示 double
F 表示 float
M 表示 decimal
U 表示 uint
UL 表示 ulong