static destructor

后端 未结 6 1721
情歌与酒
情歌与酒 2020-12-04 17:46

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

I am wondering if there is static destructor?

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 18:01

    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.

    If your program should do a specific action at the end of execution (like a transactional database engine, flushing its cache), it's going to be far more difficult to correctly handle than just a piece of code that runs at the end of normal execution of the process. You have to manually handle crashes and unexpected termination of the process and try recovering at next run anyway. The "static destructor" concept wouldn't help that much.

提交回复
热议问题