The C# as Keyword
Be very aware that explicit casting is evaluated at runtime, not compile time. Therefore, if you were
to author the following C# code:
// Ack! You can't cast frank to a Hexagon!
Hexagon hex = (Hexagon)frank;
you would receive a runtime error, or more formally a runtime exception. Chapter 7 will examine
the full details of structured exception handling; however, it is worth pointing out for the time being
when you are performing an explicit cast, you can trap the possibility of an invalid cast using the
try and catch keywords (again, don’t fret over the details):
Do'stlaringiz bilan baham: |