Should IDisposable be applied cascadingly?

前端 未结 9 1574
灰色年华
灰色年华 2020-12-31 06:48

This is a rather basic question, however I\'m still struggling with it a little.

IDisposable is implemented, when you want to enable the user of an object to free un

9条回答
  •  粉色の甜心
    2020-12-31 07:01

    You should do this, since it's the only way for the user of your class to make sure that the internally held resource is properly disposed.

    However, the pattern used for Dispose() may be slightly different than what is commonly written, in this case, since you don't have to differentiate between unmanaged and managed resources (your encapsulated resource is always treated as a "managed" resource).

    I wrote a detailed blog post on this specific topic - Encapsulating IDisposable Resources.

提交回复
热议问题