Implementing IDisposable on a subclass when the parent also implements IDisposable

后端 未结 5 1928
無奈伤痛
無奈伤痛 2020-12-01 08:03

I have a parent and child class that both need to implement IDisposable. Where should virtual (and base.Dispose()?) calls come into p

5条回答
  •  一生所求
    2020-12-01 08:35

    The child class should override the virtual Dispose, do any disposing specific to the subclass, and call the superclass' Dispose, which in turn will do its own work.

    EDIT: http://davybrion.com/blog/2008/06/disposing-of-the-idisposable-implementation/ is the pattern I follow in such cases. Not the 'Disposable' class specifically, but the inheritance and overrides.

提交回复
热议问题