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

CHAPTER 18 CONVERSIONS

Reference Conversions

As you well know by now, reference type objects comprise two parts in memory: the reference and the data.

Part of the information held by the reference is the type of the data it is pointing at.

A reference conversion takes a source reference and returns a reference pointing at the same place in the heap but “labels” the reference as a different type.

For example, the following code shows two reference variables, myVar1 and myVar2, that point to the same object in memory. The code is illustrated in Figure 18-17.

To myVar1, the object it references looks like an object of type B—which it is.

To myVar2, the same object looks like an object of type A.

Even though it is actually pointing at an object of type B, it cannot see the parts of B that extend A and therefore cannot see Field2.

The second WriteLine statement would therefore cause a compile error.

Notice that the “conversion” does not change myVar1.

class A { public int Field1; }

class B: A { public int Field2; }

class Program

{

static void Main( )

{

B myVar1 = new B();

Return the reference to myVar1 as a reference to a class A.

 

A myVar2 = (A) myVar1;

 

 

 

Console.WriteLine("{0}", myVar2.Field1);

//

Fine

 

Console.WriteLine("{0}", myVar2.Field2);

//

Compile error!

}

 

 

 

 

 

}

 

myVar2 can’t see Field2.

 

 

448

CHAPTER 18 CONVERSIONS

Figure 18-17. A reference conversion returns a different type associated to the object.

Implicit Reference Conversions

Just as there are implicit numeric conversions that the language will automatically perform for you, there are also implicit reference conversions. These are illustrated in Figure 18-18.

All reference types have an implicit conversion to type object.

Any interface can be implicitly converted to an interface from which it is derived.

A class can be implicitly converted to

Any class in the chain from which it is derived

Any interface that it implements

Figure 18-18. Implicit conversions for classes and interfaces

449

CHAPTER 18 CONVERSIONS

A delegate can be implicitly converted to the .NET BCL classes and interfaces shown in Figure 18-19. An array, ArrayS, with elements of type Ts, can be implicitly converted to the following:

The .NET BCL class and interfaces shown in Figure 18-19.

Another array, ArrayT, with elements of type Tt, if all of the following are true:

Both arrays have the same number of dimensions.

The element types, Ts and Tt, are reference types—not value types.

There is an implicit conversion between types Ts and Tt.

Figure 18-19. Implicit conversions for delegates and arrays

450

CHAPTER 18 CONVERSIONS

Explicit Reference Conversions

Explicit reference conversions are reference conversions from a general type to a more specialized type.

Explicit conversions include

Conversions from an object to any reference type

Conversions from a base class to a class derived from it

The explicit reference conversions are illustrated by reversing each of the arrows in Figures 18-18 and 18-19.

If this type of conversion were allowed without restriction, you could easily attempt to reference members of a class that are not actually in memory. The compiler, however, does allow these types of conversions. But when the system encounters them at run time, it raises an exception.

For example, the code in Figure 18-20 converts the reference of base class A to its derived class B and assigns it to variable myVar2.

If myVar2 were to attempt to access Field2, it would be attempting to access a field in the “B part” of the object, which doesn’t exist—causing a memory fault.

The runtime will catch this inappropriate cast and raise an InvalidCastException exception. Notice, however, that it does not cause a compile error.

Figure 18-20. Invalid casts raise runtime exceptions.

451

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