I use Jon Skeet'Skeet's versions version of a thread safe Singleton with fully lazy instantiation in C#:
public sealed class Singleton
{
// Thread safe Singleton with fully lazy instantiation á la Jon Skeet:
// http://csharpindepth.com/Articles/General/Singleton.aspx
Singleton()
{
}
public static Singleton Instance
{
get
{
return Nested.instance;
}
}
class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
internal static readonly Singleton instance = new Singleton();
}
}
It works wonders for me! It's really easy to use, just get the instance from the Instance property like so; SingletonName instance = SingletonName.Instance;