I was just curious about the differnce. After i read your article, i tried out my little study on differnce and it seems like prefix casting uses “castclass” function in the IL code generated while “as” casting would use isinst.
Reading about these IL commands, made me wunder again.
Did you happen to check out the differnce in IL ?
]]>according to reports I’ve seen, the performance of as- and prefix casts have largely converged as of .net 3.5. It would be interesting to run tests on mono as well.
At this point, I think people should use whatever convention they think is better from a sofware engineering standpoint. In most cases, I think it’s better to get an exception early rather than face a null pointer deference later. On the other hand, there are definitely cases where the as-cast semantics are exactly what you want.
]]>Can you provide some code sample to support your claim ??
Thanks
Anu
But also keep in mind that the as operator will only work for classes and interfaces. You must use the prefix cast when working with an enum or struct. The is operator, however, will work with enums and structs.
]]>