Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 18 CONVERSIONS

Explicit Numeric Conversions

You’ve seen that the implicit conversions automatically convert from the source expression to the target type because there is no possible loss of data. With the explicit conversions, however, there is the possibility of losing data—so it’s important for you as the programmer to know how a conversion will handle that loss if it occurs.

In this section, you will look at each of the various types of explicit numeric conversions. Figure 18-10 shows the subset of explicit conversions shown in Figure 18-8.

Figure 18-10. The explicit numeric conversions

Integral to Integral

Figure 18-11 shows the behavior of the integral-to-integral explicit conversions. In the checked case, if the conversion loses data, the operation raises an OverflowException exception. In the unchecked case, any lost bits go unreported.

Figure 18-11. Integer type to integer type explicit conversions

444

CHAPTER 18 CONVERSIONS

float or double to Integral

When converting a floating-point type to an integer type, the value is rounded toward 0 to the nearest integer. Figure 18-12 illustrates the conversion conditions. If the rounded value is not within the range of the target type, then

The CLR raises an OverflowException exception if the overflow checking context is checked.

C# does not define what its value should be if the context is unchecked.

Figure 18-12. Converting a float or a double to an integer type

decimal to Integral

When converting from decimal to the integer types, the CLR raises an OverflowException exception if the resulting value is not within the target type’s range. Figure 18-13 illustrates the conversion conditions.

Figure 18-13. Converting a decimal to an integer type

445

CHAPTER 18 CONVERSIONS

double to float

Values of type float occupy 32 bits, and values of type double occupy 64 bits. When a double is rounded to a float, the double type value is rounded to the nearest float type value. Figure 18-14 illustrates the conversion conditions.

If the value is too small to be represented by a float, the value is set to either positive or negative 0.

If the value is too large to be represented by a float, the value is set to either positive or negative infinity.

Figure 18-14. Converting a double to a float

float or double to decimal

Figure 18-15 shows the conversion conditions for converting from floating-point types to decimal.

If the value is too small to be represented by the decimal type, the result is set to 0.

If the value is too large, the CLR raises an OverflowException exception.

446

CHAPTER 18 CONVERSIONS

Figure 18-15. Converting a float or double to a decimal

decimal to float or double

Conversions from decimal to the floating-point types always succeed. There might, however, be a loss of precision. Figure 18-16 shows the conversion conditions.

Figure 18-16. Converting a decimal to a float or double

447

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]