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 5995865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:01:19+00:00 2026-05-23T00:01:19+00:00

I was researching on the singleton pattern for C# I found this example from

  • 0

I was researching on the singleton pattern for C# I found this example from the msdn website.

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

   private Singleton(){}

   public static Singleton Instance
   {
      get 
      {
         return instance; 
      }
   }
}

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.

I am not pretty sure when will the memory will get allocated to

private static readonly Singleton instance 

1)Will it happen when the Instance property is called or even before it?

2) I need to force the class to create a new memory sometimes to purge its content. Is it safe to do so using set ?

set
{
instance = null;
}
  • 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-23T00:01:20+00:00Added an answer on May 23, 2026 at 12:01 am

    In the example code you provided, the singleton instance will be created at the time of the first access to the class. This means for your example at the time when Instance gets called for the first time.

    More insight you can find in Jon Skeet’s article Implementing the Singleton Pattern in C#, see Method 4.

    Basically, in order to achieve truly lazy behaviour something like

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

    is enough.

    (But anyway, the full and much better overview can be found in the mentioned article.)

    EDIT
    Actually, as it follows from the mentioned article, the instance is not guaranteed to be created at the first access, because of BeforeFiledInit mark. You need to add an empty static constructor, this way it’s guaranteed to be lazy. Otherwise the instance will get created at some unspecified time between the program start and first access. (.NET runtime 2.0 is known to have more eager strategy, so you’ll probably not get the lazy behaviour.)

    Quoting from the said article:

    The laziness of type initializers is only guaranteed by .NET when the type isn’t marked with a special flag called beforefieldinit. Unfortunately, the C# compiler […] marks all types which don’t have a static constructor […] as beforefieldinit.

    EDIT 2
    If you want the singleton object to clean up, you should include this functionality into the class Singleton itself.

    Removing the property value will effectively make your class not a singleton any more! Imagine that someone accesses the singleton and gets the singleton instance. After that you set the property to null. The existing object of the Singleton class won’t disappear, because there’s still a reference to it! So the next time you access the Instance property, yet another instance of the Singleton class would be created. So you lost two things: your object is not a singleton any more (because you have got 2 instances existing at the same time), and the memory hasn’t been cleared either.

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

Sidebar

Related Questions

I'm currently researching how to add the Unit of Work pattern to my existing
I've been researching how to do this and I can't figure out what I
I'm researching how to build some tools in my website and as I'm looking
I've been researching this throughout SO and some people said that this error is
I've been researching this for a while but I have to ask: isn't it
Currently I'm researching the best design pattern to implement for a windows form application
I have been researching for this and read different opinions but i wanted to
I've been researching this for days. There is a piece of code I am
No luck after a few days of researching this one. Should be simple, but
I have been researching for a while and have actually created a prototype ASP.NET

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.