#447 – Use as Operator to Get At an Object’s Interfaces
If a class implements an interface, you can assign a variable whose type is that class directly to a variable of the interface’s type. Cow bossie = new Cow("Bossie", 12); IMoo viaMoo = bossie;...
View Article#578 – Using the as Operator to Do Type Conversions
The as operator attempts to convert an expression to a specified type, returning the value of null if the conversion fails. It behaves similarly to doing the conversion using a cast, but does not...
View Article#579 – Typical Pattern for Using as Operator
When you use the as operator to attempt to convert an expression to a particular type, you typically follow the pattern shown below. In most cases, you have a variable of a base class that stores some...
View Article#580 – as Operator Can Generate Compile-Time Errors
The as operator can perform reference conversions between types in a type hierarchy, returning null if the conversion is not possible. But there are some cases when the compiler knows that no...
View Article#582 – Use the as Operator to Unbox to a Nullable Type
You can box regular value types or their equivalent nullable types (e.g. int and int?) and the boxed values will either be null or be of the underlying value type. You can unbox these values to a...
View Article#1,064 – Getting Around Inability to Explicitly Convert Type Parameters
You can’t explicitly convert a type parameter to either a value type or a reference type (you can convert to an interface). To get around this restriction, you can use the as operator on the type...
View Article