jeudi 13 septembre 2018

Is This Singleton Declaration Incorrect?

I'm trying to diagnose a potential thread-safety issue found in my program, and have some suspicions that a singleton object written to provide data is not initialized correctly. The object's initialization is as follows:

public class Singleton
{
    public static readonly Singleton Instance = new Singleton();

    private Singleton()
    {
    }
}

The code that wants to request data from the instance would directly access Instance. Which is not in-line with basically every piece of sample singleton code I see, where a getter is used to access a private static instance. Could this be causing thready-safety issues in my program?

Aucun commentaire:

Enregistrer un commentaire