0

I want to implement simple Singleton and after some investigation have following to working fine. I test it using a simple console app but will be helpful if someone else can comment on it. The reason I've a doubt because new instance of Singleton is created within a static constructor and not sure if that's has any side effects.

 sealed class SingletonEx
{
    public static readonly SingletonEx Instance;

    static SingletonEx()
    {
        if (null == Instance)
        {
            Instance = new SingletonEx();

        }
    }
    private SingletonEx() { }


}

In case you're curious I found http://csharpindepth.com/Articles/General/Singleton.aspx quite helpful on this topic.

user2177565
  • 89
  • 1
  • 2
  • 8
  • possible duplicate of [What is the use of static constructors?](http://stackoverflow.com/questions/4506990/what-is-the-use-of-static-constructors) – Preston Guillot Jul 03 '14 at 22:15
  • @PrestonGuillot Although link you provided has related info I don't see above code pattern been discussed as part of conversation. – user2177565 Jul 03 '14 at 23:05
  • There's all the information about any side effects of static constructors there, and discussion of using a static constructor to instantiate a singleton is covered in a question linked in the comments, http://stackoverflow.com/questions/7095/is-the-c-sharp-static-constructor-thread-safe. Your code only adds a null guard that needn't be there. – Preston Guillot Jul 03 '14 at 23:09
  • Duplicate of http://stackoverflow.com/questions/7095/is-the-c-sharp-static-constructor-thread-safe unless you can be more specific about "side effects" – bmm6o Jul 03 '14 at 23:54

1 Answers1

0

This is not a typical usage for static constructor, although it is possible to do so. Side effect would be that constructor would run before first using of any SingletonEx instance will be used in your code. Having a static constructor wouldn't change much about the "Singleton" behaviour. But, pay attention that initilization would happend in another stage than it would usually be if it wern't static.

I'd be worried for the following line:
public static readonly SingletonEx Instance;
Here are your problems:
public - Means it could be changed outside of the class.Because the Singleton instance is referenced by a private static member variable, the instantiation does not occur until the class is first referenced by a call to the Instance property. This solution therefore implements a form of the lazy instantiation property, as in the Design Patterns form of Singleton. When implement design pattern follow as much as you can on it's design.
readonly - shouldn't be here either. It is usually editable, unless you have a good reason for that.

Inbali
  • 107
  • 1
  • 7
  • "Means it could be changed outside of the class" If it weren´t `readonly`, true. But it **is** `readonly` in order to always have the exact same instance - which is the entire purpose of a singleton. So, yes, `readonly` is absolutely neccessary here. – MakePeaceGreatAgain Dec 18 '20 at 12:26