I read Jon Skeet's definitive post on how to implement C# singleton and am following below pattern. Note that I do not have an empty ctor. My Ctor may do some work such as creating and populating an array of strings (or it might create some objects and assign them to private variables etc.):
public class MyClass
{
/// <summary>
/// Get singleton instance of this class.
/// </summary>
public static readonly MyClass Instance = new MyClass();
/// <summary>
/// a collection of strings.
/// </summary>
private string[] strings;
private MyClass()
{
this.strings = new string[]
{
"a",
"b",
"c",
"d",
"e"
};
}
public void MyMethod()
{
// tries to use this.strings.
// can a null ref exception happen here when running multithreaded code?
}
}
is above threadsafe? I ask because I have similar code running on asp.net appserver and get null ref exception in the logs (not sure if the null ref is related to above - I think not - and the call stack in log is not helpful).