Recently read in a article on dotnetpearls.com here saying that static ctors take a substantial amount of perfomance hit.
Could not fathom why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think "substantial" is an overstatement in most use cases.
Having a static constructor (even if it does nothing) affects type initialization time due to the presence/absence of the beforefieldinit flag. There are stricter guarantees about timing when you have a static constructor.
For most code, I’d suggest this doesn’t make much difference – but if you’re tight-looping and accessing a static member of a class, it might. Personally I wouldn’t worry about it too much – if you have a suspicion that it’s relevant in your real application, then test it rather than guessing. Microbenchmarks are very likely to exaggerate the effect here.
It’s worth noting that .NET 4 behaves somewhat differently to previous versions when it comes to type initialization – so any benchmarks should really show the different versions in order to be relevant.