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

  • SEARCH
  • Home
  • 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 889531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:34:58+00:00 2026-05-15T13:34:58+00:00

Is this singleton implementation correct and thread-safe? class Class { public static readonly Class

  • 0

Is this singleton implementation correct and thread-safe?

class Class
{
    public static readonly Class Instance;

    static Class()
    {
        Instance = new Class();
    }

    private Class() {}
}
  • 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-15T13:34:58+00:00Added an answer on May 15, 2026 at 1:34 pm

    Technically, your version should work. However, I would not recommend exposing a public field within your Singleton class, and prefer using a Property (with a getter only). This will help future-proof your API if you need to make changes later. I also recommend sealing any singleton implementation, as subclassing a singleton class is almost always a bad idea and problematic.

    I would, personally, use the following in C#, if you’re targetting .NET 3.5 or earlier:

    public sealed class Singleton
    {
        static readonly Singleton instance = new Singleton();
    
        public static Singleton Instance
        {
            get
            {
                return instance;
            }
        }
    
        static Singleton() { }
        private Singleton() { }
    }
    

    If you’re using .NET 4, you can make this even easier for yourself via Lazy<T>:

    public sealed class Singleton
    {
         private static readonly Lazy<Singleton> instance = new Lazy<Singleton>( () => new Singleton() );
         private Singleton() {}
         public static Singleton Instance { get { return instance.Value; } }
    }
    

    The .NET 4 version also has the advantage of being fully lazy – even if your Singleton class has other static methods which are used prior to the access of the “Instance” property. You can do a fully-lazy .NET 3.5- version, as well, by using a private, nested class. Jon Skeet demonstrated this on his blog.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In other words, is this Singleton implementation thread safe: public class Singleton { private
I implemented a Singleton pattern like this: public sealed class MyClass { ... public
i used to create an instance of a singleton class like this: $Singleton =
I'm implementing a singleton class as follows: static Singleton* _singletonInstance; @implementation Singleton +(void)initialize {
I'm building a singleton that has a ThreadLocal as an instance variable, this singleton
I have a singleton class for global access to config information. This singleton class
I have a class that is a singleton. This singleton is instantiated using GCD
I have a singleton implemented like this: class Test123(object): _instance = None def __new__(cls,
I was reading up on singleton class design in C# on this great resource
I'm very new to multi-threading and for some reason this class is giving me

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.