try...catch...finally...bloggg....: Typecasting using the 'as' keyword in C#

try...catch...finally...bloggg....

Friday, March 25, 2005

Typecasting using the 'as' keyword in C#

Recently read about what's the difference in using 'as' keyword to do the type conversion.

expression as type

is equivalent to:

expression is type ? (type)expression : (type)null

except that expression is evaluated only once.

Note that the as operator only performs reference conversions and boxing conversions. The as operator cannot perform other conversions, such as user-defined conversions, which should instead be performed using cast expressions.

So for people who use the regular type casting and the as operator interchangably beware... if you expect some type and you are guaranteed to get the same use the regular type casing. Stay away from 'as' as long as your requirement strongly needs it.

0 Comments:

Post a Comment

<< Home


·