Performance of: if (OBJECT_INSTANCE is TYPE)
I am working both in C# and ActionScript 3. Both languages 开发者_如何学JAVAare type-aware, so you can do verifications such as:
if (some_object_instance is SomeClassName_ThatIs_SomeType)
I'm doing these kind of verifications in a few places. Might be a dumb question, but I will ask it anyway, and I want answers from both camps, C# and ActionScript:
What goes on behind the scenes? Is it Reflection? If is, isn't this a long verification which might degrade performance if done in thousands of loops? And by "degrade performance" I mean, is it more intensive than say if (Math.sqrt(8) > Math.sin(10))
In C# (or better put: ILCode), it is compiled to an instruction. Good read for that Is is as or is as is? by Eric Lippert.
Well, it's reflection and it's not. An "is" statement will cause the runtime to examine the manifest of the assembly containing the type, to inspect the inheritance hierarchy. Programmatic reflection would do the same thing. However, in this case it's done using a far more lightweight and specific means built into the runtime than would be available from the System.Reflection namespace.
精彩评论