The Singleton Pattern in .NET and C#

Published 11 October 07 09:18 AM | john 

I just got bitten by a static initialization problem - again.  There are two things you've got to be careful of in .NET when using statics (i) threading issues and (ii) initialization ordering issues.  I'm not going to go into a great long discussion about the issues as it's been covered really well in other places.  The rules are simple, but there are a couple of tricky wrinkles, and it's one of those areas that you don't code to all the time, so by the time you need to know the "really important thing" you've forgotten it.  Anyway, I found Implementing the Singleton Pattern in C# to be the most helpful memory jogger.  Bart de Smet's blog shows how to do deterministic initialization order by using the RuntimeHelpers.RunClassConstructor method.

Comments

No Comments
Anonymous comments are disabled