Implicit numeric conversions for C #

Source: Internet
Author: User

Implicit numeric conversions consist of the following:

From sbyte type to short,int,long,float,double, or decimal type.

From byte type to short,ushort,int,uint,long,ulong,float,double, or decimal type.

From the short type to the int,long,float,double, or the decimal type.

From ushort type to int,uint,long,ulong,float,double, or decimal type.

from type int to long,float,double, or decimal type.

From UINT type to long,ulong,float,double, or decimal type.

From type long to float,double, or decimal type.

From ulong type to float,double, or decimal type.

From char type to ushort,int,uint,long,ulong,float,double, or decimal type.

From float type to double type.

Among them, conversion from int,uint, or long to float, and from long to double may result in a decrease in precision, but will never cause a loss of quantity. Other implicit numeric conversions do not have any loss of information.

In conjunction with the range of value types we learn in data types, we can see that implicit numeric conversions are actually conversions from a low precision numeric type to a high-precision numeric type.

From the above 10 we can see that there is no implicit conversion to the char type, which means that other integer values cannot be automatically converted to the Char type. We need to pay special attention to this point.

The following procedure gives an example of an implicit numeric conversion.

Program Listing 6-1:

Using System;
Class Test
{public
  static void Main ()
  {
    byte x=16;
    Console.WriteLine ("X={0}", x);
    UShort Y=x;
    Console.WriteLine ("Y={0}", y);
    y=65535;
    Console.WriteLine ("Y={0}", y);
    float z=y;
    Console.WriteLine ("Z={0}", z);
  }

The output of the program will be:

x=16;

y=16;

y=65535;

z=65535;

If we add a sentence after the statement in the above program:

y=y+1;

When you recompile the program, the compiler will give an error message:

Can not implictly convert type ' int ' to type ' ushort '

This shows that there is no implicit conversion from the integer type 65536 to the unsigned short integer y.

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.