Possible Duplicate:
Direct casting vs 'as' operator?
Casting vs using the 'as' keyword in the CLR
object myObject = "Hello world.";
var myString = myObject as string;
object myObject = "Hello world.";
var myString = (string)myObject;
I have seen type conversion done both ways. What is the difference?
It only checks the runtime type of
myobject. If itsstring, only then it cast asstring, else simply returnsnull.This also looks for
implicitconversion to string from the source type. If neither the runtime type isstring, nor there isimplicitconversion, then it throws exception.Read Item 3: Prefer the
isorasOperators to Casts from Effective C# by Bill Wagner.