This section describes the conversion between C # numeric types, including testbasic () functions.
C # conversion between numeric types
The value types mentioned here include byte, short, Int, long, fload, and double. According to the order, values of various types can be automatically converted backward. For example, if a short type data value is assigned to an int type variable, the short value is automatically converted to an int type value and then assigned to the int type variable. For example:
- Private void testbasic (){
- Byte A = 1; short B = A; int c = B;
- Long d = C; float E = D; double F = E;
- This. textbox1.text = "";
- This. textbox1.appendtext ("Byte A =" + A. tostring () + "\ n ");
- This. textbox1.appendtext ("Short B =" + B. tostring () + "\ n ");
- This. textbox1.appendtext ("int c =" + C. tostring () + "\ n ");
- This. textbox1.appendtext ("long d =" + D. tostring () + "\ n ");
- This. textbox1.appendtext ("float E =" + E. tostring () + "\ n ");
- This. textbox1.appendtext ("double F =" + F. tostring () + "\ n ");
- }
The running result shows that the values of each variable are 1. Of course, their types are system. byte ...... System. Double type. Now let's try. What if we reverse the order of values? Append the following statement to the testbasic () function:
- Int G = 1;
- Short H = g;
- This. textbox1.appendtext ("H =" + H. tostring () + "\ n ");
Result compilation error:
G: \ projects \ Visual C # \ convert \ form1.cs (118): The type "int" cannot be implicitly converted to "short, the first line of form1.cs is the row where short H = G is located.
At this time, if we insist on conversion, we should use forced type conversion, which is often mentioned in C language, that is, "(type name) variable name to forcibly convert data. The preceding example is modified as follows:
- Short G = 1;
- Byte H = (byte) g; // convert the value of the Short Type G to the short type and then assign it to the variable H.
- This. textbox1.appendtext ("H =" + H. tostring () + "\ n ");
After compilation, H = 1 is output and the conversion is successful.
However, if we use forced conversion, we have to consider another problem: the short type range is-32768 ~ 23767, while the byte type ranges from 0 ~ 255. What will happen if the variable g size exceeds the byte range? Let's rewrite the code again and change the value to 265, which is 10 larger than 255.
- Short G = 265; // 265 = 255 + 10
- Byte H = (byte) g;
- This. textbox1.appendtext ("H =" + H. tostring () + "\ n ");
There is no compilation error, but the running result is not h = 265, but h = 9.
Therefore, when performing conversion, we should note that the data to be converted cannot exceed the range of the target type. This is not only reflected in the conversion of the Multi-byte data type (relative, short in the above example) to the smaller byte type (relative, byte in the above example, it is also reflected in the conversion of byte 129 to sbyte between the same number of bytes of the signed type and the unsigned type.