Please help me decide whether this should be good or bad idea to declare a Singleton variable in Global.asax file. (It is not actually singleton pattern, I just want to make sure that only one instance is created for this class.)
public class Global : System.Web.HttpApplication
{
private HTML5Video dailyHtml5Video;
public HTML5Video DailyHtml5Video
{
get
{
if (dailyHtml5Video==null)
{
dailyHtml5Video = new HTML5Video();
}
return dailyHtml5Video;
}
}
}
Will this cause any thread-related issues or other possible issues?
1 Answer 1
It is not thread safe. You can use Lazy type to create a variable of HTML5Video in a thread safe manner.
Lazy<HTML5Video> dailyHtml5Video = new Lazy<HTML5Video>(() => new HTML5Video());
public HTML5Video DailyHtml5Video
{
get {return dailyHtml5Video.Value;}
}
You can read about different versions of Singleton pattern here http://csharpindepth.com/articles/general/singleton.aspx