What’s the difference between cast syntax and using the as operator?
Using the <code>as</code> operator differs from a cast in C# in three important ways: <ol> <li> It returns <code>null</code> when the variable you are trying to convert is not of the requested type or in it's inheritance chain, instead of throwing an exception. </li> <li> It can only be applied to reference type variables converting to reference types. </li> <li> Using <code>as</code> will not perform user-defined conversions, such as implicit or explicit conversion operators, which casting syntax will do. </li> </ol><p> There are in fact two completely different operations defined in IL that handle these two keywords (the <code>castclass</code> and <code>isinst</code> instructions) - it's not just "syntactic sugar" written by C# to get this different behavior. The <code>as</code> operator appears to be slightly faster in v1.0 and v1.1 of Microsoft's CLR compared to casting (even in cases where there are no invalid casts which would severely lower casting's performance due to exceptions).
[Author: Jon Skeet]