Singleton class design in C#, are these two classes equivalent?

Posted by Oskar on Stack Overflow See other posts from Stack Overflow or by Oskar
Published on 2010-04-12T07:28:50Z Indexed on 2010/04/12 7:33 UTC
Read the original article Hit count: 357

I was reading up on singleton class design in C# on this great resource and decided to go with alternative 4:

public sealed class Singleton1
{
    static readonly Singleton1 _instance = new Singleton1();

    static Singleton1()
    {
    }

    Singleton1()
    {
    }

    public static Singleton1 Instance
    {
        get
        {
            return _instance;
        }
    }
}

Now I wonder if this can be rewritten using auto properties like this?

public sealed class Singleton2
{
    static Singleton2()
    {
        Instance = new Singleton2();
    }

    Singleton2()
    {
    }

    public static Singleton2 Instance { get; private set; }
}

If its only a matter of readability I definitely prefer the second version, but I want to get it right.

© Stack Overflow or respective owner

Related posts about c#

Related posts about singleton