C # numeric type conversion

Source: Internet
Author: User

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:

  1. Private void testbasic (){
  2. Byte A = 1; short B = A; int c = B;
  3. Long d = C; float E = D; double F = E;
  4. This. textbox1.text = "";
  5. This. textbox1.appendtext ("Byte A =" + A. tostring () + "\ n ");
  6. This. textbox1.appendtext ("Short B =" + B. tostring () + "\ n ");
  7. This. textbox1.appendtext ("int c =" + C. tostring () + "\ n ");
  8. This. textbox1.appendtext ("long d =" + D. tostring () + "\ n ");
  9. This. textbox1.appendtext ("float E =" + E. tostring () + "\ n ");
  10. This. textbox1.appendtext ("double F =" + F. tostring () + "\ n ");
  11. }

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:

  1. Int G = 1;
  2. Short H = g;
  3. 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:

  1. Short G = 1;
  2. Byte H = (byte) g; // convert the value of the Short Type G to the short type and then assign it to the variable H.
  3. 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.

  1. Short G = 265; // 265 = 255 + 10
  2. Byte H = (byte) g;
  3. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.