As we all know, statements like "int A = 10; short B = A" cannot be compiled because cannot implicitly convert type 'int' to 'short '. I wrote a statement like "Short = 10", which is no problem, that is, no error or warning. Why? Does the compiler automatically add forced type conversion for me? In order to uncover these puzzles, I did some tests, so I had this article.
C # There are 11 basic numeric data types, of which 8 are Integer Data Types (byte, sbyte, short, ushort, Int, uint, long, ulong ), three decimals (double, float, decimal ). first, I tested the eight integer types as follows:
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;
}
}
Generated IlCodeAs follows:
. Method private hidebysig static void main (string [] ARGs) cel 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: sts1_int8 consoleapplication1.program: _ sbyte
Il_000f: LDC. i4.s 20
Il_0011: sts0000int16 consoleapplication1.program: _ short
Il_0016: LDC. i4.s 20
Il_0018: stsfld uint16 consoleapplication1.program: _ ushort
Il_001d: LDC. i4.s 20
Il_001f: sts1_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: sts0000int64 leleapplication1.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: sts0000int16 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: sts1_int64 leleapplication1.program: _ long2
Il_0058: Ret
} // End of method program: Main
We found that the code generated by _ byte = 20 and _ byte = (byte) 20 is exactly the same, and the code generated by _ long = 20 and _ long = 20l is also the same. Therefore, combined with some other tests (omitted here), we come to the following conclusion:
1. The benefits of _ byte = 20 and _ byte = (byte) 20 are the same.
2. The benefits of _ long = 20 are the same as those of _ long = 20l. "l" in 20l only plays a role in compilation.
3. the compiler automatically checks whether the value directly assigned exceeds the maximum value of this type. (_ byte = 256 may cause compilation errors)
Next, we will test double, float, and decimal: (The comments will cause compilation to fail)
class Program
{< br> Public static float _ float;
Public static double _ double;
Public static decimal _ decimal;
static void main (string [] ARGs)
{< br> _ float = 5;
// _ float = 5.0;
_ float = 5f;
// _ float = 5 m;
_ Double = 5;
_ Double = 5.0;
_ Double = 5f;
// _ Double = 5 m;
_ Decimal = 5;
// _ Decimal = 5.0;
_ Decimal = 5.0 m;
// _ Decimal = 5.0f;
}
}
The IL code is as follows:
. Method private hidebysig static void main (string [] ARGs) cel 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
Conclusion:
1. Different from the integer type, the corresponding numeric type must be specified when the decimal type is directly assigned, or the default numeric type can be implicitly converted to the variable type.
2. The efficiency of _ float = 5 is the same as that of _ float = 5f. "F" also only plays a role in compilation.
3. The value assignment of the decimal type is somewhat different from that of other types. The decimal constructor has nine public constructor methods.
The above is my simple analysis of the value type. If you have any shortcomings or errors, please note.
Appendix:
L indicates long
D Indicates double
F indicates float
M indicates decimal
U indicates uint
Ul indicates ulong