1

I've been reading about the Singleton pattern in C# and how thread safety can be a concern, especially when it comes to lazy initialization. I'm aware of the Lazy<T> class which can be used to create a thread-safe lazy-initialized singleton. However, for educational purposes, I'd like to understand how one might implement this without relying on Lazy<T>.

Here's what I've tried so far:

public sealed class Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
}
  1. Is the above code a proper thread-safe lazy-initialized Singleton in C#?
  2. Are there potential pitfalls or performance concerns with this approach?
  3. Are there alternative methods to achieve this without Lazy<T>?

Any insights or suggestions would be appreciated!

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • 2
    You might find this interesting: [Double-checked locking in .NET](https://stackoverflow.com/questions/394898/double-checked-locking-in-net). Or this: [The need for volatile modifier in double checked locking in .NET](https://stackoverflow.com/questions/1964731/the-need-for-volatile-modifier-in-double-checked-locking-in-net). – Theodor Zoulias Aug 14 '23 at 22:11
  • 1
    @TheodorZoulias Thank you for answer I really like what you shared. – Fatih Furkan Çambel Aug 14 '23 at 22:43
  • 2
    See also https://csharpindepth.com/Articles/Singleton – stuartd Aug 14 '23 at 22:54
  • 2
    @stuartd I think that the `thread-safety` and `lazy-initialization` tags are definitely on point. Personally I wouldn't have seen this question without these tags. – Theodor Zoulias Aug 14 '23 at 23:14
  • Fatih Furkan you could consider closing this question as a duplicate of one of the linked questions. Although it's not an exact duplicate, the topic has been thoroughly discussed, and getting an answer here is not very likely. Alternatively you could consider posting a [self-answer](https://stackoverflow.com/help/self-answer) if you want, with your research. – Theodor Zoulias Aug 15 '23 at 09:05
  • this resource offers enough answers for me https://csharpindepth.com/Articles/Singleton and other links. I want to could close this question as a duplicate of one of the linked questions. – Fatih Furkan Çambel Aug 15 '23 at 15:21
  • 1
    There is a "Close" button under the tags of the question. Then you can select "Duplicate", and paste the URL of the duplicate question. – Theodor Zoulias Aug 15 '23 at 15:40
  • 1
    I didn't find "Close" button under the tags of the question. – Fatih Furkan Çambel Aug 15 '23 at 16:19
  • 1
    Strange. Maybe you don't have enough reputation points. I was assuming that the author of a question can always close it as a duplicate, single-handedly. I am closing it for you. – Theodor Zoulias Aug 16 '23 at 00:03

0 Answers0