How should I inherit IDisposable?

前端 未结 6 1091
南笙
南笙 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:00

    It depends on your interface, but I'd lean toward #2. If you have two implementations of ISomeInterface and only one needs disposing, then there's the possibility you need to refactor.

    In general when you're binding to an interface, it's better to have that interface inherit IDisposable rather than the base class; if your interface doesn't inherit IDisposable, you must cast to IDisposable to dispose of the object, and that runs the risk of an InvalidCast...

提交回复
热议问题