C# Singleton question

Posted on 2011-05-11
Last Modified: 2013-12-16
Hi Experts,
I would like to use a Singleton, but, kind of contradictingly, I'd like to use the Singleton sometimes, and also be able to create a new instance of the same class.  I have the following code that creates a Singleton.  Is there a way to modify this so that I can create a new one?

public sealed class Singleton
    static readonly Singleton instance=new Singleton();

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Singleton()


    public static Singleton Instance
            return instance;

Open in new window

Question by:thready
    LVL 4

    Accepted Solution

    just make your private constructor - public.
    You'll keep your static instance of this class, but will be able to create another one by calling constructor.

    The question is - how when you need to create new instance? Do you want to call Singleton.Instance and sometimes get a new instance?
    If yes, I'll suggest you to implement Lazy Load pattern and ClearCache() method...
    LVL 4

    Expert Comment

    Doh,spelling mistakes

    The question is - when you need to create new instance?
    LVL 1

    Author Closing Comment

    Thank you.
    LVL 7

    Expert Comment

    It would appear that you don't need a singleton at all.  One, if not the(?), purpose of a singleton is that it's designed such that only a single instance of itself is ever created/used.  If you re-initialize the instance, then by definition your object is not a singleton but instead you just have some psuedo-static class that's probably difficult to understand and correctly use.

    There are few places where the singleton pattern is the correct one.  The most common example is some kind of logging utility that doesn't necessarily impact your program, but instead it just logs output.  It doesn't interact with the rest of your code.

    Most of the time, when you find yourself thinking, "A singleton will solve this problem!," all you really need is just a static class.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now