静态析构函数静态、函数

2023-09-02 10:43:47 作者:我比你爸还疼你▲-/m

C#有静态构造函数里面做一些初始化。 (可能做一些非托管资源初始化)我想知道是否有静电destuctor?

c# has static constructor which do some initialization. (Likely Do some unmanaged resource initialization) I am wondering if there is static destuctor?

推荐答案

没有,没有。

一个静态的析构函数理应会在执行过程的结束时运行。当一个进程死亡,与之相关联的所有内存/句柄将得到释放由操作系统。

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.

如果你的程序应该做的,在执行结束特定动作(如事务处理数据库引擎,刷新其高速缓存),这将是更为困难,正确处理不仅仅是一张code运行时该过程的正常执行的结束。你必须手动处理事故和意外终止的过程中,并尝试在下次运行恢复反正。 静态析构函数的概念不会帮助那么多。

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.