samedi 12 mars 2016

Theard sefefy in static methods and variables

I was in impression that static initialization is thread safe by default. Is that correct? Also consider the below example of singleton pattern.

public sealed class SingletonDemo
{
    private static readonly SingletonDemo instance = new SingletonDemo();    
    private SingletonDemo()
    {}
    public static SingletonDemo Instance
    {
        get
        {
            return instance;
        }
    }
} 

Is above example thread safe? If yes why? If No why?

Aucun commentaire:

Enregistrer un commentaire