The casting requires runtime checks too, so there's very little difference.
The as
returns null or the casted object, so it may have the tiny extra overhead of assigning a reference [EDIT: But it seems not; see the blog linked below]
(If the compiler knows the type at compile time, it will optimise the cast away, but it will also optimise the as
away. Also, if a cast fails it'll generate an exception which is very slow indeed, but not important if you expect the cast to always succeed.)
Technically, for a cast the IL produced is (for the example of casting object
to string
):
castclass string
and for an as
:
isinst string
See the following blog post where someone has done some detailed timings (and found that isinst
is marginally faster, but not by any amount that you'd care about):
http://m3mia.blogspot.co.uk/2007/11/comparing-isinst-to-castclass.html