Head First C# by Andrew Stellman

Head First C# by Andrew Stellman

Author:Andrew Stellman
Language: eng
Format: epub
Publisher: O'Reilly Media
Published: 2020-04-08T00:00:00+00:00


IWorker pearl = new HoneyManufacturer(); NectarCollector irene = (NectarCollector)pearl;

is an invalid cast that tries to cast an object to a variable that doesn’t match its type.

The “is” keyword lets you convert types safely.

Luckily, the is keyword is safer than casting with parentheses. It lets you check that the type matches, and only casts the reference to a new variable if the types match.

if (pearl is NectarCollector irene) { /* Code that uses a NectarCollector object */ }

This code will never throw an InvalidCastException because it only executes the code that uses a NectarCollector object if pearl is a NectarCollector.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.