I have done some research on this issue, but still quite puzzled.
I read one article that says static constructor can be a big performance
hit, it even gives some code fragment, which, accoring to that article,
could show a big difference betweeen using static constructor or not. But
when I run his code, I found no significant diifference at all.
For this reason, I even checked the assemly code of using static
constructor, and I can not find any thing that will cause performornce
degradation.
Or course, lazy initialization is good, but not all cases really care that
much about startup time, so I doubt if the performance issue of static
constructor is exagerated.
Could someone shed some light on this?
Jon Skeet [C# MVP] - 03 May 2007 19:55 GMT
> I have done some research on this issue, but still quite puzzled.
> I read one article that says static constructor can be a big performance
[quoted text clipped - 9 lines]
>
> Could someone shed some light on this?
For classes with static constructors, the type initializer can't be
executed until the first time a member of the class is *actually*
referenced, rather than just at (say) the start of a method where a
member of the class *might* be referenced.
I wouldn't say it's a particularly significant performance hit even in
the most extreme situations I've looked at, but it *is* noticeable if
you have (say) a very tight loop which just calls a cheap static method
on a class with a static constructor.
Of course, this may have been optimised a bit in .NET 2.0...

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too