SIGN UP MEMBER LOGIN:    
ARTICLE

Singleton Patterns in C# Revised

Posted by Trevor Misfeldt Articles | Visual C# December 16, 2002
I am coming from the Java world, where synchronizing the Singleton creation is common practice.
Reader Level:
Download Files:
 

I noticed an error in the Singleton pattern article by Rajesh V.S., dated 12/05/2001. I believe that the code to create the new instance should be synchronized using a 'lock'. I am coming from the Java world, where synchronizing the Singleton creation is common practice. I wrote a test program to try it out, and  the instance count kept creeping up as I ran the program. I have included the source code below. Just alternately add and remove the lock in the Singleton.getInstance() method to see the effect of synchronization on the number of instances of Singleton that are created.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor