How should I inherit IDisposable?

前端 未结 6 1082
南笙
南笙 2021-02-04 01:31

Class names have been changed to protect the innocent.

If I have an interface named ISomeInterface. I also have classes that inherit the interface, FirstClass

6条回答
  •  后悔当初
    2021-02-04 02:04

    If there is a reasonable chance that an abstract entity (interface or abstract class) might need to be disposable, it should implement it. Stream, for example doesn't itself need IDisposable, nor does IEnumerator...

    An abstract base class may be simpler, as you can have a default (empty) implementation of Dispose() then, and possibly the finalizer / Dispose(bool) pattern, i.e.

    void IDisposable.Dispose() { Dispose(true); GC.SuppressFinalize(this); }
    protected virtual void Dispose(bool disposing) {}
    ~BaseType() {Dispose(false);}
    

提交回复
热议问题