Difference Between C# Cast Syntax and the AS Operators
You know what? Sometimes, the most obvious and easy questions are those that are the most frequently asked. One of such questions about C# is the difference between the cast syntax and the as operator. This is sometimes referred to as prefix-casting and as-casting.
Without giving much thought to the difference, you may think that having two constructs for casting is somewhat redundant. However, that's not true. C# is an extremely elegant language and every construct has its place and helps you provide clean code. Moreover, these casts have dedicated operations in the Intermediate Language (i.e. the .NET language).
Let's first list the differences. So, what we have is this.
1. The as operator returns null when casting is impossible (for example, the variable you are trying to convert is not of the requested type). Prefix casting will instead throw an exception.
2. The as operator can be applied only when you want to cast a reference type variable to a reference type. The prefix casting is free of this constraint.
3. The as operator cannot perform user-defined conversions (for example, explicit or implicit conversion operators). A prefix cast can perform these types of conversions.
However, the impact on your code can be more than the above three points describe. First things first. Prefix casting yields to a more reliable code. Why? Because if the cast fails, an exception is thrown right at the moment of casting. With the as operator, no exception is thrown. However, now you have a null reference and once you use the variable, a null reference exception is thrown. Unfortunately, this can be quite far from the place where the cast was performed. Because of this, applications using the as operator can be more difficult to debug.
Some programmers consider the old C style casting ugly. Personally I don't have anything against the prefix casting (probably for our passion towards the C programming language). However, there is one serious benefit in favor of using the as operator. It's way too fast when compared to the prefix casting. We have not done a scientific test but we've heard that the difference is on the factor of five. Imagine now prefix casting used in loops. A real slowdown for your code.
Thus, whenever possible, eliminate the casting at all, especially in long loops. If, however, casting is impossible to avoid, then use the as operator. But try to do a check right after casting. This will give you a fast and a clean code and a code that is elegant to read and easy to debug.
Without giving much thought to the difference, you may think that having two constructs for casting is somewhat redundant. However, that's not true. C# is an extremely elegant language and every construct has its place and helps you provide clean code. Moreover, these casts have dedicated operations in the Intermediate Language (i.e. the .NET language).
Let's first list the differences. So, what we have is this.
1. The as operator returns null when casting is impossible (for example, the variable you are trying to convert is not of the requested type). Prefix casting will instead throw an exception.
2. The as operator can be applied only when you want to cast a reference type variable to a reference type. The prefix casting is free of this constraint.
3. The as operator cannot perform user-defined conversions (for example, explicit or implicit conversion operators). A prefix cast can perform these types of conversions.
However, the impact on your code can be more than the above three points describe. First things first. Prefix casting yields to a more reliable code. Why? Because if the cast fails, an exception is thrown right at the moment of casting. With the as operator, no exception is thrown. However, now you have a null reference and once you use the variable, a null reference exception is thrown. Unfortunately, this can be quite far from the place where the cast was performed. Because of this, applications using the as operator can be more difficult to debug.
Some programmers consider the old C style casting ugly. Personally I don't have anything against the prefix casting (probably for our passion towards the C programming language). However, there is one serious benefit in favor of using the as operator. It's way too fast when compared to the prefix casting. We have not done a scientific test but we've heard that the difference is on the factor of five. Imagine now prefix casting used in loops. A real slowdown for your code.
Thus, whenever possible, eliminate the casting at all, especially in long loops. If, however, casting is impossible to avoid, then use the as operator. But try to do a check right after casting. This will give you a fast and a clean code and a code that is elegant to read and easy to debug.



If the cast is supposed to succeed, using the prefix cast is faster than using as and then checking for null - it's basically just one less branch in your code.
If the cast is supposed to NOT succeed at least some of the time, casting via "as" and checking for null is faster than letting the prefix cast throw the exception and handle it.
It comes down to using these constructs for what they're designed: the prefix cast is for when the cast is supposed to succeed, and failure would be an error. The "as" cast is for when the cast is supposed NOT to succeed some of the time: failure is not an error but a normal case you handle yourself via checking for null.