Why is casting faster than reflection in .NET?

Reflection has to go at runtime and determine what properties etc. the object has at runtime. Casting tells the application that it should expect that an object has X properties and should function in a certain way.


Reflection is slow because you are querying the assembly's metadata whereas casting simply changes the type of the object you are referencing.

The assembly's metadata is a useful store of information but that information is best used at compilation time rather than at execution time. That metadata is used by the compiler for static type checking (among other things). You are using that same metadata to look up type information at execution time (which is fine if you have no other choice) which is significantly slower than casting.


Casting tells the runtime you "know" the type of a particular object. While you may possibly be wrong the runtime believes you and doesn't take the extra time needed to go check the assembly's meta data.