I see code similar to the following sprinkled about some native WPF controls:
static MyControl {
Type typeFromHandle = typeof(MyControl);
// Which is used in various places
SomeProperty.OverrideMetadata(typeFromHandle, ...);
CommandManager.RegisterClassInputBinding(typeFromHandle, ...);
EventManager.RegisterClassHandler(typeFromHandle, ...);
}
Seems like the following code would have the same performance:
static MyControl {
SomeProperty.OverrideMetadata(typeof(MyControl), ...);
CommandManager.RegisterClassInputBinding(typeof(MyControl), ...);
EventManager.RegisterClassHandler(typeof(MyControl), ...);
}
Does this approach offer any benefit performance wise when JIT-ing the code or during runtime?
Caching of
typeofwill give you a small performance boost. The following page gives the benchmarks:If you
typeofa lot, or care about nanoseconds, then this might matter to you!