Performance of TypeCasting

.net, c#, performance, types

Solution

It may be measurable if it's being done billions of times with very little other work. I don't know whether the CLR will effectively cache the fact that the cast worked, so it doesn't need to do it again - if it doesn't do so now, it might do so in a later release. It might do so in the 64 bit JIT but not the 32 bit version, or vice versa - you get the idea. I doubt that it would make a significant difference in normal code though.

Personally I like the readability of the second form more though, and that's more important by far.

Problem

is there any measurably performance difference between ``` ((TypeA) obj).method1(); ((TypeA) obj).method2(); ((TypeA) obj).method3(); ``` and ``` var A = (TypeA) obj; A.method1(); A.method2(); A.method3(); ``` when used alot of times? I often see something like ``` if (((TextBox)sender).Text.Contains('.') || ((TextBox)sender).Text.Contains(',')) ``` and wonder if this is a waste of performance.

Original source