Casting within inheritance hierarchies
Casting between types is subtly different from converting between types. Casting is between similar types, like between a 16-bit integer and a 32-bit integer, or between a superclass and one of its subclasses. Converting is between dissimilar types, such as between text and a number.For example, if you need to work with multiple types of
stream, then instead of declaring specific types of stream like
FileStream, you could declare an array of
Stream, the supertype of
In the previous example, you saw how an instance of a derived type can be stored in a variable of its base type (or its base's base type, and so on). When we do this, it is called implicit casting.
The opposite of implicit casting is explicit casting, and you must use parentheses around the type you want to cast into as a prefix to do it:
Program.cs, add a statement to assign the