How to access this Singleton object?

Hi, from this link, 
the code implementation for multi-thread,below, how would one access that instance static variable?  I mean, the class Singleton is sealed but it's not static and the constructor is private so I can't use new to instantiate it, right?  So can I just use Singleton.instance?  So I can access the static instance even if the class Singleton is not static?

also, how does this lock object(synroot) work?  Thank you.

using System;

public sealed class Singleton
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}

   public static Singleton Instance
         if (instance == null) 
            lock (syncRoot) 
               if (instance == null) 
                  instance = new Singleton();

         return instance;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
All you need is this line.

Singleton s = Singleton.Instance;

Open in new window

käµfm³d 👽Commented:
so I can't use new to instantiate it, right?

So can I just use Singleton.instance?

So I can access the static instance even if the class Singleton is not static?
Yes. That's exactly how static members work. Something declared as static is tied to a class, not an instance. Therefore, to access a static member, you have to use the syntax:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
As they explain on the page you linked:
This approach ensures that only one instance is created and only when the instance is needed. Also, the variable is declared to be volatile to ensure that assignment to the instance variable completes before the instance variable can be accessed. Lastly, this approach uses a syncRoot instance to lock on, rather than locking on the type itself, to avoid deadlocks.

This double-check locking approach solves the thread concurrency problems while avoiding an exclusive lock in every call to the Instance property method. It also allows you to delay instantiation until the object is first accessed. In practice, an application rarely requires this type of implementation. In most cases, the static initialization approach is sufficient.

So in other words.  Singleton.Instance is only creatable by one and only one thread at a time (because of the lock mechanism).

käµfm³d 👽Commented:
how does this lock object(synroot) work?
I cannot tell you how it works on a technical level--I keep meaning to look this up myself--but at a high level what is going on is that your lock statement is initiating a lock against the syncRoot object. While this lock is acquired, no other thread can enter the section that is wrapped by the lock's braces. Any thread that tries to acquire the lock while it is taken will have to wait until the lock is released. The lock statement requires an object be passed to it, but you never want to lock on this, even though it is technically possible. The standard practice is to lock on a new object that you create as a private field (that's what synroot is above), though from time to time it may be more appropriate to lock on some other field (I cannot really tell you what scenarios that might be).
lapuccaAuthor Commented:
Thank you Kaufm.  Your answer on Lock and all were perfectly clear.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.