Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4005496
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:24:02+00:00 2026-05-20T08:24:02+00:00

I have a number of classes doing different things but using the same Singleton

  • 0

I have a number of classes doing different things but using the same Singleton pattern from http://www.yoda.arachsys.com/csharp/singleton.html

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

Singleton()
{
}

public static Singleton Instance
{
    get
    {
        lock (padlock)
        {
            if (instance==null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}
}

Does anyone have a neat way I can reuse as much of the common Singleton code as possible between the different classes?

For example if I have SingletonJob1 and SingletonJob2 I’d like to be able to change the code in only place if I move to one of the other Singleton patterns.

Edit: Yes as people have pointed out Method 5 from http://www.yoda.arachsys.com/csharp/singleton.html is less code. I did read to the end of the page! I chose method 2 because the Singleton objects relate to hardware devices and I want only want a couple of them initialised and used in any given run of the program. Method 5 will initialise them all straight away.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:24:03+00:00Added an answer on May 20, 2026 at 8:24 am

    Is there any reason you’re using that version rather than the simpler one which just initializes the instance in the declaration?

    public class Singleton
    {
        private static Singleton instance = new Singleton();
        public static Singleton Instance { get { return instance; } }
    
        // Only use this if you really need it - see the page for details
        static Singleton() {}
    
        private Singleton()
        {
            // I assume this logic varies
        }
    }
    

    This pattern is sufficiently short that I don’t think it’s much of a problem to include it everywhere.

    I would urge you to consider whether you really need that many singletons; they’re generally not great for testability etc.

    EDIT: If you really, really want laziness and you’re using .NET 4, you can get it with the 6th pattern which is on the singleton page’s new home:

    public sealed class Singleton
    {
        private static readonly Lazy<Singleton> lazy =
            new Lazy<Singleton>(() => new Singleton());
    
        public static Singleton Instance { get { return lazy.Value; } }
    
        private Singleton()
        {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of classes with the same functions but with different logic.
I have a project with a number of different classes querying and modifying data
I have a number of data classes representing various entities. Which is better: writing
I have a python module that defines a number of classes: class A(object): def
In Ruby I have often written a number of small classes, and had a
I'm working with a number of 'helper' classes, which affectively have a bunch of
Does Information Hiding mean I should minimize the number of properties my classes have?
I have a number of macros written for Visual Studio 2005, but they have
I'm using .NET 3.5. We have some complex third-party classes which are automatically generated
Suppose I have a number of related classes that all have a method like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.