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

C H A P T E R 18

■ ■ ■

Conversions

What Are Conversions?

Implicit Conversions

Explicit Conversions and Casting

Types of Conversions

Numeric Conversions

Reference Conversions

Boxing Conversions

Unboxing Conversions

User-Defined Conversions

The is Operator

The as Operator

435

CHAPTER 18 CONVERSIONS

What Are Conversions?

To get an understanding of what conversions are, let’s start by considering the simple case in which you declare two variables of different types and then assign the value of one (the source) to the other (the target). Before the assignment can occur, the source value must be converted to a value of the target type. Figure 18-1 illustrates type conversion.

Conversion is the process of taking a value of one type and using it as the equivalent value of another type.

The value resulting from the conversion should be the same as the source value—but in the target type.

Figure 18-1. Type conversion

For example, the code in Figure 18-2 shows the declaration of two variables of different types.

var1 is of type short, a 16-bit signed integer that is initialized to 5. var2 is of type sbyte, an 8-bit signed integer that is initialized to the value 10.

The third line of the code assigns the value of var1 to var2. Since these are two different types, the value of var1 must be converted to a value of the same type as var2 before the assignment can be performed. This is performed using the cast expression, which you'll see shortly.

Notice also that the value and type of var1 are unchanged. Although it is called a conversion, this only means that the source value is used as the target type—not that the source is changed into the target type.

Figure 18-2. Converting from a short to an sbyte

436

CHAPTER 18 CONVERSIONS

Implicit Conversions

For certain types of conversions, there is no possibility of loss of data or precision. For example, it’s easy to stuff an 8-bit value into a 16-bit type with no loss of data.

The language will do these conversions for you automatically. These are called implicit conversions.

When converting from a source type with fewer bits to a target type with more bits, the extra bits in the target need to be filled with either 0s or 1s.

When converting from a smaller unsigned type to a larger unsigned type, the extra, most significant bits of the target are filled with 0s. This is called zero extension.

Figure 18-3 shows an example of the zero extension of an 8-bit value of 10 converted to a 16-bit value of 10.

Figure 18-3. Zero extension in unsigned conversions

For conversion between signed types, the extra most significant bits are filled with the sign bit of the source expression.

This maintains the correct sign and magnitude for the converted value.

This is called sign extension and is illustrated in Figure 18-4, first with 10 and then with –10.

Figure 18-4. Sign extension in signed conversions

437

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