Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2006

Tip: Looking for answers? Try searching our database.

Singleton using static constructor

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Veeneman - 29 Sep 2006 23:43 GMT
Why do these questions always come up on Friday afternoon?

I'm starting to use GoF singleton classes in my projects. Right off the bat,
I've run into a surprise. I thought that a Singleton could be implemented
using a static constructor, using code like this, based on version #4 at
http://www.yoda.arachsys.com/csharp/singleton.html:

public sealed class MyClass
{
   private static MyClass m_MyClass = new MyClass();

   static MyClass()
   {
   }

   public static MyClass GetSingleton()
   {
       return m_MyClass;
   }
}

According to the C# Language Specification (10.11 Static constructors), a
static constructor can't be called directly, so I'd use a call like this to
get the single instance of the class:

MyClass myObject = MyClass.GetSingleton();

However, .NET allows me to call the constructor directly:

MyClass myObject = new MyClass();

What's more, its allowing me to create multiple objects from the class. So
the Singleton approach is clearly failing.

What am I missing? Better yet, what's the simplest thread-safe approach to
implementing a Singleton in .NET 2.0? Thanks.

Signature

David Veeneman
Foresight Systems

Joanna Carter [TeamB] - 29 Sep 2006 23:52 GMT
| However, .NET allows me to call the constructor directly:
|
[quoted text clipped - 5 lines]
| What am I missing? Better yet, what's the simplest thread-safe approach to
| implementing a Singleton in .NET 2.0? Thanks.

You forgot to hide the default instance constructor :

public sealed class MyClass
{
 private static MyClass m_MyClass = new MyClass();

 static MyClass()
 {
 }

 ///////////////////////  don't forget this :-)
 private MyClass() {}
 //////////////////////

 public static MyClass GetSingleton()
 {
   return m_MyClass;
 }
}

Also see Jon's comments about thread safety.

Joanna

Signature

Joanna Carter [TeamB]
Consultant Software Engineer

David Veeneman - 30 Sep 2006 02:11 GMT
Thanks, Joanna--that took care of it. So .NET wasn't calling the static
constructor--it was calling the implicit public constructor? And adding the
private constructor prevents the implicit public constructor, so all is
well. Right?
Joanna Carter [TeamB] - 30 Sep 2006 07:49 GMT
| Thanks, Joanna--that took care of it. So .NET wasn't calling the static
| constructor--it was calling the implicit public constructor? And adding the
| private constructor prevents the implicit public constructor, so all is
| well. Right?

Sort of :-)

If you check out Jon's writings on singletons you will see that there is a
case for initialising the static field in the static constructor as this is
guaranteed to be called only once, just before the first call to the
GetInstance method, therefore ensuring the instance gets created in a
threadsafe manner. The code that you show doesn't create an instance, so you
needed to either check for null in the GetInstance method, which is not
threadsafe, or simply add a call to the instance constructor in the static
constructor, which is threadsafe.

Joanna

Signature

Joanna Carter [TeamB]
Consultant Software Engineer


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.