65.9K
CodeProject is changing. Read more.
Home

Simple Singleton Pattern in C#

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (4 votes)

Oct 27, 2011

CPOL
viewsIcon

10727

The problem with alternative 3 and 4 is that it is not multi-threaded.Two threads may check for null, then the two will create the new instance. The race condition only happens at the first accesses. If a single thread accesses the object, then later many threads do the access, there is no...

The problem with alternative 3 and 4 is that it is not multi-threaded. Two threads may check for null, then the two will create the new instance. The race condition only happens at the first accesses. If a single thread accesses the object, then later many threads do the access, there is no problem, so it is hard to cause that bug... but it is hard to discover it later. EDIT: Sorry. Alternative 3 works well in multi-threaded environments, as it is reading a read-only field that's set during initialization. When I saw it, I got the impression the property was initializing it.