Is this a valid, lazy, thread-safe Singleton implementation for C#?

Posted by Matthew on Stack Overflow See other posts from Stack Overflow or by Matthew
Published on 2010-04-11T00:05:28Z Indexed on 2010/04/11 0:13 UTC
Read the original article Hit count: 198

I implemented a Singleton pattern like this:

public sealed class MyClass {

    ...

    public static MyClass Instance {
        get { return SingletonHolder.instance; }
    }

    ...

    static class SingletonHolder {
        public static MyClass instance = new MyClass ();
    }
}

From Googling around for C# Singleton implementations, it doesn't seem like this is a common way to do things in C#. I found one similar implementation, but the SingletonHolder class wasn't static, and included an explicit (empty) static constructor.

Is this a valid, lazy, thread-safe way to implement the Singleton pattern? Or is there something I'm missing?

© Stack Overflow or respective owner

Related posts about c#

Related posts about singleton