I want to have a Singleton that will be automatically created when the program starts.
What I mean by "auto instantiated" is that the code in Singleton should run itself when the program starts, without any calls or declarations by other code.
So, I want something like the following to create an instance and write out "MySingleton Instantiated" when the program starts (do nothing without the main code) ...
static class MySingleton
{
private static MySingleton self = new MySingleton();
protected MySingleton()
{
System.Console.WriteLine("MySingleton Instantiated");
}
}
except that this does not work, since C # will only initialize the static members of the class when necessary, i.e. when they are accessed by / etc.
So what should I do? It can be done?
I have not done this personally with C ++ (I have not used C ++ for a while), but I am sure that this can be done in C ++, but not sure about C #.
. .
, , - ...
( ), () (, PClass).
PClass , PClasses... , ...
( PClass )... , () , (Singleton) - .
, ( ) ... , .
, .
PS. , , ... , , , #. .