The first one (a "direct" or "C-style" cast) throws an exception if the cast is invalid. It is also the only way to perform actual type conversion on the object. (Note that type conversion is different from casting, because casting merely changes the type of the variable, whereas type conversion gives you a *different type of object.)
The second one (no particular name, although you can call it "try cast" as it's called in VB.NET) evaluates to null
instead of throwing an InvalidCastException
. (Because of this behavior it only works for reference types).
No major performance issues compared to each other.
You use as
only if you expect that your result might not be valid. Otherwise, use the first one.
By the way, MSDN might be helpful for parts of your question:
The as
operator is like a cast operation. However, if the conversion is not possible, as returns null
instead of raising an exception. Consider the following expression:
expression as type
It is equivalent to the following expression except that expression is evaluated only one time.
expression is type ? (type)expression : (type)null
Note that the as
operator only performs reference conversions and boxing conversions. The as
operator cannot perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.