UPDATED:
I read here https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff650316(v=pandp.10)?redirectedfrom=MSDN about 3 different ways of implementing the singleton in c#. I'm particularly interested in understanding this line:
Because the instance is created inside the Instance property method, the class can exercise additional functionality (for example, instantiating a subclass), even though it may introduce unwelcome dependencies.
I understand that although lazy instantiation is not thread safe, still because it only instantiates it on demand it thus avoids instantiation every time the class is loaded. I also notice that in Lazy-instantiation I can use a non-default constructor and pass parameters whereas in Static.. But say I didn't mind it's instantiated every time it's loaded and I'm happy with a default constructor, why would I choose Lazy over static or even over the multi-threaded approach? Is there something Lazy can get me that static and multi-threaded can't? Can one be sub classed and the other not?
Also, that page seems to be saying that static initialisation is also thread-safe? Why is that? And why are they proceeding to a third multi-threaded approach if the latter is fine too?
Because the Singleton instance is referenced by a private static member variable, the instantiation does not occur until the class is first referenced by a call to the Instance property. This solution therefore implements a form of the lazy instantiation property, as in the Design Patterns form of Singleton.
(Nonetheless, I don't understand the first part of this para: if instantiation here also occurs only once as they say in a couple of paragraphs before:
"In this strategy, the instance is created the first time any member of the class is referenced."
how is it different than the lazy approach?)
So to summarise, my question is what DOES static come to cure over the first, lazy approach and why would I still choose Lazy other than the reason that it allows me to use a none-default constructor?
Whilst I'm at it:
Consider the following two pairs of examples of Lazy vs Static, my question is, within each pair, how are they different from one another? I can see in one, the 'get' is nested within the GetInstance/Instance method but does it make any difference?
public class LazySingleton
{
private static LazySingleton _LSInstance;
private LazySingleton() { }
public static LazySingleton GetInstnace
{
get{
if (_LSInstance == null)
{
_LSInstance = new LazySingleton();
}
return _LSInstance;
}
}
}
public class LazySingleton
{
private static LazySingleton _LSInstance { get; set; }
private LazySingleton() { }
public static LazySingleton GetInstnace() {
if (_LSInstance == null)
_LSInstance = new LazySingleton();
return _LSInstance;
}
}
=======================================================
public class Singleton
{
private Singleton()
{
}
private static Singleton _LSInstance = new Singleton();
public static Singleton GetInstnace
{
get{
return _LSInstance;
}
}
}
public class Singleton
{
private Singleton() { }
private static Singleton _LSInstance = new Singleton();
public static Singleton GetInstnace()
{
return _LSInstance;
}
}
I'd really appreciate a response that can clarify this for me, thank you!