public sealed class Singleton
{
private static Singleton instance = null;
private static readonly object padlock = new object();
private Singleton()
{
}
public static Singleton Instance
{
get
{
lock (padlock)
{
if (instance==null)
{
instance = new Singleton();
}
return instance;
}
}
}
}
Eller med hjälp av klassen Lazy<T>():
public class Singleton
{
private readonly static Lazy _instance = new Lazy(() => new Singleton());
public static Singleton Instance
{
get
{
return _instance.Value;
}
}
}
Läs mer om singleton på denna sida:
http://www.yoda.arachsys.com/csharp/singleton.html