Wednesday, February 9, 2011

Singleton C#

Non thread-safe

public class MyStorage
{
private static MyStorage instance;

public static MyStorage Instance
{
get {
if (instance == null)
instance = new MyStorage();

return instance;
}
}

private MyStorage()
{
// do stuff
InitStuff();

bool result;
_overwrite = bool.TryParse( ConfigurationManager.AppSettings["Overwrite"], out result) ? result : false;
}

...
}

No comments: