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.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s