Why there is no static destructor?
I try to avoid using static classes in my production code as they cant be injected, there is no control over default initialization and lastly you can't really clean up your resources implicitly as there is no destructor for static objects. Additionaly开发者_如何学Python, you cannot implement the IDisposable for a static class as well, so sounds like static classes are never good for being a wrapper around unmanaged resources... It totally seems like singletons are a better solution to replace the use of static classes directly in that case. But my question is - why doesn't the compiler support static destruction, after all what difference does it make for the GC to keep track of references to a static object vs an instance?
There's no such thing as a "static object". But all static variables in all types loaded in any app domain are treated as GC roots until the app domain is unloaded.
If you want to do things when the app domain is unloaded, you could subscribe to AppDomain.DomainUnload and AppDomain.ProcessExit.
If you want a static destructor, won't an AppDomain.DomainUnload
event work?
精彩评论