Why is there no static destructor?

I try to avoid using static classes in my production code because they cannot be introduced, there is no control over the default initialization, and finally you cannot really clear your resources implicitly, since there is no destructor for static objects. Also, you cannot implement IDisposable for a static class, so sounds like static classes are never good for wrapping around unmanaged resources ... It is clear that singletones are the best solution to replace using static classes directly in in this case. But my question is: why does the compiler not support static destruction, because what difference does GC make to track references to a static object and an instance?

+4
source share
2 answers

There is no such thing as a "static object". But all static variables in all types loaded into any application domain are treated as GC roots until the application domain is unloaded.

If you want to do something when the application domain is unloaded, you can subscribe to AppDomain.DomainUnload and AppDomain.ProcessExit .

+7
source

If you want a static destructor, will the AppDomain.DomainUnload event not work?

+2
source

All Articles