How and when are c# Static members disposed?

后端 未结 4 1967
忘了有多久
忘了有多久 2020-12-08 19:11

I have a class with extensive static members, some of which keep references to managed and unmanaged objects.

For instance, the static constructor is called as soon

4条回答
  •  渐次进展
    2020-12-08 19:41

    If you really want to have static members which keep references to unmanaged objects just create a method for disposing the unmanaged objects and "force" consumer to use it on exit.

    By "force" I mean document your class with a paragraph that states "when" and "why" to use this "dispose" method. Do it either if you are the sole consumer (or your code...) or you plan to distribute your class. Also try to use a somehow descriptive name (to that "dispose" method) such as "DisposeStatics", "AlwaysDispose", "DisposeAtEnd" etc.

提交回复
热议问题