C # instance / object initialization at program start

I am a C / C ++ programmer recently working in C # and I am trying to do some fancy initialization things that I came across with some problems.

The best and easiest example that I can come up with is that I want to create an "Eager" Singleton - one that is created immediately when the program starts, but without me you need to go to the main function of the program and say "Singleton.Instance () "like the first. I read a static instance, and it looks like it should be called or created before creating the static object, so I tried to create a static variable that instantiated the object, but that didn't work. (I could not find documentation on when static variables are created or initialized).

Any pointers?

Thanks!

Edit: after some additional research, I think I can accomplish what I'm looking for with one block of code using reflection

+4
source share
1 answer

The static constructor is called only when the class that contains the constructor is first called. Therefore, when you want your initialization code to start when the program starts, you must explicitly refer to the class that contains the constructor in your start code (for example, the Main method).

+2
source

All Articles