The following are the bytes of memory for an integer type:
int 4 bytes Memory usage -2147483648~2147483647
UINT integer 0-4294967295 with no minus sign
Short 2 bytes -32768~32767
UShort 2 bytes 0-65535
Long 8 bytes -9223372036854775808~9223372036854775807
ULONG 8 bytes
SByte 1 Bytes-128-127
BYTE 1 byte 0-255
Char 2 bytes representing 1 characters
Float 4 byte 7-bit precision
Double 8 byte 15-16 precision
Decimal 16 byte 28-bit precision
Note: The ushort and char can be converted to each other because Char is also an integral type of given, which stores a numeric value, so it is a numeric type.
Case 1:
The code is as follows |
Copy Code |
static void Main (string[] args) { UShort Shortnum; Char charnum = ' a '; Shortnum = CharNum; Console.WriteLine (CharNum)//show A Console.WriteLine (shortnum);//Show 97 } |
Even though the information stored is the same, it does not represent the same way.
Note: bool and string do not have an implicit conversion! Types that have no relationship with each other cannot be cast.
Overflow checking environment: (for casting)
Case 2:
The code is as follows |
Copy Code |
static void Main (string[] args) { UShort shortnum=281; BYTE Bytenum; Bytenum = (byte) shortnum; Console.WriteLine (Bytenum)//display 25 because the data is overflow Console.WriteLine (shortnum);//Show 281 Use checked to detect if overflow Bytenum = checked ((byte) shortnum);/Unhandled Exception: System.OverflowException: arithmetic operation causes overflow. Unchecked is not detecting whether the overflow } |
Convert explicit conversion:
Note: For convert conversion is definitely to detect data overflow
The code is as follows |
Copy Code |
Convert.tobyte ()//Convert byte Convert.tochar (); Convert.ToInt16 ()//Convert short Convert.ToInt32 ();//Convert int Convert.toint64 ()//Convert Long Convert.tosbyte ()//conversion sbyte Convert.touint16 ()//conversion ushort Convert.touint32 ()//conversion UINT Convert.touint64 ()//conversion ULONG Convert.toboolean ()//convert bool Convert.tosingle ()//convert float Convert.todouble (); Convert.todecimal (); |
Case 3:
The code is as follows |
Copy Code |
static void Main (string[] args) { ushort shortnum=281; byte Bytenum; bytenum = Convert.tobyte (shortnum);// For the convert conversion, the //Unhandled exception: for sure to detect data overflow System.OverflowException: value is too large or too small for unsigned bytes. Console.WriteLine (bytenum);//Show 25, Because the data overflowed Console.WriteLine (shortnum); Display 281 } |
Note: Short*float is required for conversion, the default is to convert the short to float, because short is 2 bytes, float is 4 bytes-implicit conversion
Complex variable types:
1. Enumeration
The code is as follows |
Copy Code |
Test1: public class Enumtest { Enum days {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; static void Main () { int x = (int) Days.sun; int y = (int) Days.fri; Console.WriteLine ("Sun = {0}", x); Console.WriteLine ("Fri = {0}", y); } } /* Output: Sun = 0 Fri = 5 */ |
Note: enum days {sat=1, Sun, Mon, Tue, Wed, Thu, Fri}; The default type that forces the serial number to start at 1 instead of 0.enum is int. But it cannot be a char type
Test2:
The code is as follows |
Copy Code |
public class EnumTest2 { Enum Range:long {Max = 2147483648L, Min = 255L}; static void Main () { Long x = (long) Range.max; Long y = (long) range.min; Console.WriteLine ("Max = {0}", x); Console.WriteLine ("Min = {0}", y); } } /* Output: Max = 2147483648 Min = 255 */ |