The difference between as and () operators in C#


In C#, you have two different ways to perform type casting (a.k.a. explicit type conversion) – as and () operators. The difference between the two operators is not limited to just syntax, they work differently. It is subtle enough to trap even experienced developers, especially those coming from other languages, such as Delphi where meaning of these operators is completely opposite.

So what is the difference? When () operator cannot convert the type, it will throw InvalidCastException; however, as operator will silently return null. Why should you care about it? Consider the following code

01 public class BaseType {
02     // …
03 }
04
05 public class DerivedType: BaseType {
06     public void DerivedMethod() {}
07 }
08
09 public class Program {
10     public static Main() {
11         BaseType myBase = new BaseType();
12         Class1.Method1(myBase);
13     }
14 }
15
16 public class Class1 {
17     public static void Method1(BaseType myBase) {
18         DerivedType myDerived = myBase as DerivedType;
19         Method2(myDerived);
20     }
21
22     public static void Method2(DerivedType myDerived) {
23         myDerived.DerivedMethod();
24     }
25 

Notice in line 18 we are using as operator to convert BaseType to DerivedType. Can you tell what will happen this program runs and Main method passes an object of BaseType to Method1? The code will fail with NullReferenceException on line 23, inside Method2. The only meaningful information you will get from this exception is that it occurred in Method2. If you are running a debug build with symbols, you will also get the source file name and the line number from it. This doesn’t tell you anything about the actual cause of the problem, which was an invalid call to Method2.

The more code and method calls you have between the as operator and the line which tries to access its result, the more difficult it becomes to determine the original cause of the problem. However, if we change the code in line 18 to use the () operator, trying to pass BaseType object to Method1 will fail right there in line 18 with InvalidCastException whose message will say that an instance of BaseType could not be converted to DerivedType. The important difference between these two failures is that exception occurs much closer to the place in code where the actual problem lies and it provides specific details about the problem.

The as operator was designed to allow writing code similar to the one below and improve performance by allowing you to avoid double type casting you would have with the is operator and InvalidCastException you would have with the () operator:

26     void Method3(BaseType myBase) {
27         DerivedType myDerived = myBase as DerivedType;
28         if (myDerived != null) {
29             Method2(myDerived);
30         }
31     }
32 }

Conclusion

Using () operator helps you to improve quality of your code by helping it to fail fast. as operator has a special purpose and should not be used as a substitute for ().

Information and Links


Other Posts
TFS Power Tools
Microsoft SharedView

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

[…] Oleg Sych - » The difference between as and () operators in C# - In C#, you have two different ways to perform type casting (a.k.a. explicit type conversion) – as and () operators. The difference between the two operators is not limited to just syntax, they work differently. … […]