Implicit and display-type conversions for C #

Source: Internet
Author: User
The implicit conversion and display transformations are available in every language, and in C #, of course, no exception. Generally used in the occasion, a write operation expression, to maintain the overall data type consistent and accurate calculation, for example, there is a variable a is an int, but it is obtained by subtraction, then in the operation is to consider the display conversion, except the operation needs to be converted to a float or double, Then cast the result to an int, and another is to pass the parameters, you need to pay attention to what needs to show the transformation, or to assign values to the collection should also pay attention to the type of problem.
1. Implicit type conversions
The so-called implicit conversion, which is the system default conversion, is essentially a small storage capacity data type that is automatically converted to a large storage capacity data type. There are several:
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 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 the UINT type to the long,ulong,float,double, or decimal type.

From a long type to a 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 the float type to the double type.


2. Explicit type conversions

An explicit type cast, that is, coercion of type conversions. Contrary to implicit conversions, and casts can result in data loss.

From sbyte to Byte,ushort,uint,ulong, or char.

From byte to SByte or char.

From short to Sbyte,byte,ushort,uint,ulong, or char.

From ushort to Sbyte,byte,short, or char.

from int to Sbyte,byte,short,ushort,uint,ulong, or char.

From uint to Sbyte,byte,short,ushort,int, or char.

From long to Sbyte,byte,short,ushort,int,uint,ulong, or char.

From ulong to Sbyte,byte,short,ushort,int,uint,long, or char.

From Char to Sbyte,byte, or short.

From float to Sbyte,byte,short,ushort,int,uint,long,ulong,char, or decimal.

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

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

When the conversion has lost information or causes an exception to be thrown, the conversion is performed according to the following rules:

For conversions from one integer to another, the compiler will overflow detection for the conversion, and if no overflow occurs, the conversion succeeds, or throws a OverflowException exception. This detection is also related to whether the checked option is set in the compiler.

For conversions from float,double, or Decimal to Integer, the value of the source variable is rounded to the nearest integer value as the result of the conversion. If this integer value exceeds the range of the target type, a OverflowException exception is thrown.

For conversions from double to float, the double value is rounded to the nearest float value. If this value is too small, the result will be positive 0 or minus 0; If the value is too large, it will become
Infinite or negative infinity. If the original double value is Nan, the conversion result is also Nan.

For conversions from float or double to decimal, the source value is converted to decimal form and rounded to the 28 digits after the decimal point (if necessary). If the source value is too small, the result is 0, and if it is too large to be represented by decimals, or infinity and Nan, the InvalidCastException exception is thrown.

For conversions from decimal to float or double, the decimal value is rounded to the nearest value. This conversion may lose precision, but it will not cause an exception.

The above is the implicit and display type conversion of C # content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.