C# has two primary ways of changing the types of objects
from one type to another. There is the standard casting performed by naming the
type to cast to before the expression to cast from inside of a set of
parentheses. The other way is to use the as operator.
Listing 1: Basic Type Casting
object myIntObj = 1;
int myInt = (int)myIntObj;
Listing 2: Basic Use of the "as" Operator
object myStringObj = "Brendan Enrick";
string myString = myStringObj as string;
I am sure pretty everyone has seen both of these, but
something more important than just knowing about them is knowing what they do
and when to use each one. The goal of this article is to explain when each
should be used and why.