Casting vs using the ‘as’ keyword in the CLR
SomeClass someObject = (SomeClass) obj;
SomeClass someObject = obj as SomeClass;
With the "classic" method, if the cast fails, an exception is thrown. With the as method, it results in null, which can be checked for, and avoid an exception being thrown.
Also, you can only use "as" with reference types, so if you are typecasting to a value type, you must still use the "classic" method.
as method can only be used for types that can be assigned a
null value. That use to only mean reference types, but when .NET 2.0 came out, it introduced the concept of a nullable value type. Since these types can be assigned a
null value, they are valid to use with the