C# has static constructor which do some initialization (likely do some unmanaged resource initialization).
I am wondering if there is static destructor?
Static destructors are executed one by one in reverse order to the order of corresponding classes definition. Static destructors are always executed after software entry point and always after constructors of all global objects.
No, there isn't. A static destructor supposedly would run at the end of execution of a process. When a process dies, all memory/handles associated with it will get released by the operating system.
Destructors cannot be declared const , volatile , const volatile or static . A destructor can be declared virtual or pure virtual . If no user-defined destructor exists for a class and one is needed, the compiler implicitly declares a destructor.
Static object is an object that persists from the time it's constructed until the end of the program. So, stack and heap objects are excluded. But global objects, objects at namespace scope, objects declared static inside classes/functions, and objects declared at file scope are included in static objects.
Not exactly a destructor, but here is how you would do it:
class StaticClass { static StaticClass() { AppDomain.CurrentDomain.ProcessExit += StaticClass_Dtor; } static void StaticClass_Dtor(object sender, EventArgs e) { // clean it up } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With