Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static destructor

Tags:

c#

.net

C# has static constructor which do some initialization (likely do some unmanaged resource initialization).

I am wondering if there is static destructor?

like image 505
user496949 Avatar asked Dec 06 '10 08:12

user496949


People also ask

What 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.

Can static class have destructor?

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.

Can destructor be static C++?

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.

What is a static object in C++?

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.


1 Answers

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    } } 
like image 176
zackery.fix Avatar answered Oct 07 '22 10:10

zackery.fix