Let’s say I have a MySingleton class with a static constructor.
I want that singleton to be immediately to be created when my app start
Is there a way to do this without to have to code MySingleton.Instance.MyMethod when the app start … just for the singleton to call the static constructor? I’m not so sure if this is possible but I’m throwing the question out there.
Thanks,
Yes. Put the creation logic in a static constructor, and then put Main in that class. The static constructor is always called before any method of the class, and Main is a method of the class.
If you can’t put Main in that class then no. The static constructor will be triggered by (1) calling any static method on the class, or (2) creating an instance of the class.