O que é um padrão de modelo genérico de singleton threadsafe bom em c #
Eu tenho o seguinte padrão singleton C #, existe alguma maneira de melhorá-lo?
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()
{ }
}
Exemplo de uso preferido:
class Foo : Singleton<Foo>
{
}
Relacionado: