Singleton Patterns in C# Revised


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.

Up Next
    Ebook Download
    View all
    Learn
    View all