C # value conversion-explicit value conversion table (reference)

Source: Internet
Author: User

What is explicit conversion?

Explicit Conversion
When converting a type to another type, additional code is required to complete the conversion.

Copy codeThe Code is as follows: int n = 1;
Byte B = (byte) n; // correct, explicit conversion
Byte b2 = n; // Error

For Explicit conversions, note that the results are not necessarily what we want.

Copy codeThe Code is as follows: int n = 256;
Byte B = (byte) n; // The result is 0.

The above result is 0. Because it exceeds 255, it starts from 0;
If n is 257, B is 1;

If n is 258, B is 2;
......

The Convert and Convert classes are used to Convert data types. There are many methods, such as ToInt32, which means converting data to int. It involves a large span of types. For example, you can convert an object or string to an int type, while (int) can only convert the numeric type to an int type.

For more information, see Convert, Parse, TryParse, and (int.
Explicit numeric conversion table (from MSDN)

Slave

To

Sbyte

Byte, ushort, uint, ulong, or char

Byte

Sbyte or char

Short

Sbyte, byte, ushort, uint, ulong, or char

Ushort

Sbyte, byte, short, or char

Int

Sbyte, byte, short, ushort, uint, ulong, or char

Uint

Sbyte, byte, short, ushort, int or char

Long

Sbyte, byte, short, ushort, int, uint, ulong, or char

Ulong

Sbyte, byte, short, ushort, int, uint, long, or char

Char

Sbyte, byte, or short

Float

Sbyte, byte, short, ushort, int, uint, long, ulong, char, or decimal

Double

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

Decimal

Sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double

Remarks (from MSDN)

Explicit numeric conversion may cause loss of precision or exceptions.
When a decimal value is converted to an integer, the value is rounded to the nearest integer to zero. OverflowException is triggered if the result integer is out of the range of the target type.
When you convert a double or float value to an integer, the value is truncated. If the integer value of the result exceeds the range of the target value, the result depends on the context of the overflow check. OverflowException is triggered in the checked context. In the unchecked context, the result is an unspecified target type value.
When double is converted to float, the double value is rounded to the nearest float value. If the double value is too small or too large to accommodate the target type, the result is zero or infinite.
When float or double is converted to decimal, the source value is converted to a decimal representation and rounded to the nearest number after 28th decimal places (if needed ). Depending on the source value, the following results may be generated:
If the source value cannot be expressed as decimal because it is too small, the result will be zero.
OverflowException is triggered if the source value is NaN (non-numeric value), infinity, or cannot be expressed as decimal due to an excessively large value.
When decimal is converted to float or double, the decimal value is rounded to the nearest double or float value.

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.