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

CHAPTER 18 CONVERSIONS

Types of Conversions

There are a number of standard, predefined conversions for the numeric and reference types. The categories are illustrated in Figure 18-7.

Beyond the standard conversions, you can also define both implicit and explicit conversions for your user-defined types.

There is also a predefined type of conversion called boxing, which converts any value type to either of these:

Type object

Type System.ValueType

Unboxing converts a boxed value back to its original type.

Figure 18-7. Types of conversions

Numeric Conversions

Any numeric type can be converted into any other numeric type, as illustrated in Figure 18-8. Some of the conversions are implicit conversions, and others must be explicit.

Figure 18-8. Numeric conversions

440

CHAPTER 18 CONVERSIONS

Implicit Numeric Conversions

The implicit numeric conversions are shown in Figure 18-9.

There is an implicit conversion from the source type to the target type if there is a path, following the arrows, from the source type to the target type.

Any numeric conversion for which there is not a path following the arrows from the source type to the target type must be an explicit conversion.

The figure demonstrates that, as you would expect, there is an implicit conversion between numeric types that occupy fewer bits to those that occupy more bits.

Figure 18-9. The implicit numeric conversions

441

CHAPTER 18 CONVERSIONS

Overflow Checking Context

You’ve seen that explicit conversions have the possibility of losing data and not being able to represent the source value equivalently in the target type. For integral types, C# provides you with the ability to choose whether the runtime should check the result for overflow when making these types of conversions. It does this through the checked operator and the checked statement.

Whether a segment of code is checked or not is called its overflow checking context.

If you designate an expression or segment of code as checked, the CLR will raise an OverflowException exception if the conversion produces an overflow.

If the code is not checked, the conversion will proceed regardless of whether there is an overflow.

The default overflow checking context is not checked.

The checked and unchecked Operators

The checked and unchecked operators control the overflow checking context of an expression, which is placed between a set of parentheses. The expression cannot be a method. The syntax is the following:

checked

(

Expression

)

unchecked (

Expression

)

For example, the following code executes the same conversion—first in a checked operator and then in an unchecked operator.

In the unchecked context, the overflow is ignored, resulting in the value 208.

In the checked context, an OverflowException exception is raised.

ushort

sh = 2000;

 

 

byte

sb;

 

 

sb

= unchecked ( (byte) sh );

//

Most significant bits lost

Console.WriteLine("sb: {0}", sb);

 

 

sb

=

checked ( (byte) sh );

//

OverflowException raised

Console.WriteLine("sb: {0}", sb);

442

CHAPTER 18 CONVERSIONS

This code produces the following output:

sb: 208

Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an overflow. at Test1.Test.Main() in C:\Programs\Test1\Program.cs:line 21

The checked and unchecked Statements

The checked and unchecked operators that you just saw act on the single expression between the parentheses. The checked and unchecked statements perform the same function but control all the conversions in a block of code, rather than in a single expression.

The checked and unchecked statements can be nested to any level.

For example, the following code uses checked and unchecked statements and produces the same results as the previous example, which uses checked and unchecked expressions. In this case, however, blocks of code are affected, rather than just expressions.

byte

sb;

 

ushort sh = 2000;

 

unchecked

// Set unchecked

{

 

 

sb = (byte) sh;

 

Console.WriteLine("sb: {0}", sb);

 

checked

// Set checked

{

sb = (byte) sh;

 

 

 

Console.WriteLine("sb: {0}", sh);

}

}

443

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