Was ist ein gutes threadsicheres generisches Singleton-Vorlagenmuster in C #?
Ich habe das folgende C # Singleton-Muster. Gibt es eine Möglichkeit, es zu verbessern?
public class Singleton<T> where T : class, new()
{
private static object _syncobj = new object();
private static volatile T _instance = null;
public static T Instance
{
get
{
if (_instance == null)
{
lock (_syncobj)
{
if (_instance == null)
{
_instance = new T();
}
}
}
return _instance;
}
}
public Singleton()
{ }
}
Bevorzugtes Anwendungsbeispiel:
class Foo : Singleton<Foo>
{
}
verbunden: