Casting

Casting an instance of a derived class to a base type is always safe. As shown in the previous section, this is the cast for polymorphism and is legitimate in all circumstances. Derived types extend base types. Because derived types encompass everything about the base type, a cast that is derived to the base type is guaranteed to be safe. You can even cast a derived instance to an abstract base class. This has already been demonstrated more than once in this chapter. Casting from a derived object to a base reference provides a base view of the instance. You are limited to members of the base type. The extensions of the derived type are not visible through a base reference to a derived instance. After the cast to the base type, the base ...

Get Programming Microsoft® Visual C#® 2008: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.