1

I know that if you have a class which has a member which implements IDispose, your class should also implement IDispose. Does that apply to cases where you inherit from a class which implements IDispose?

Example:
class OwnsSocket: IDisposable
{
     ... implements dispose pattern
{

class DerivedSocketOwner: OwnsSocket
{
}

Should DerivedSocketOwner implement IDisposable as well?

I think I'm running into an issue where an object is getting disposed, but the socket has not been closed, then an async callback for the socket gets called, but some of the objects it is trying to access have been disposed of already. I'm wondering if this is the culprit.

bpeikes
  • 3,495
  • 9
  • 42
  • 80
  • 2
    You need to implement your own only if you need different behaviour of `Dispose` than in the base class – Fabio Aug 04 '21 at 19:53
  • 1
    If you inherit from a (non-abstract) disposable class, you have already implemented `Dispose()` through inheritance. You are not obligated to *override* it, if that is what you are asking. But those who use your derived class must remember to *call* `Dispose()`, even if you didn't override it. – John Wu Aug 04 '21 at 19:55
  • [What's the point of overriding Dispose(bool disposing) in .NET?](https://stackoverflow.com/questions/584549/) • [When and How to Use Dispose and Finalize in C#](https://dzone.com/articles/when-and-how-to-use-dispose-and-finalize-in-c) • [Item 17: Implement the Standard Dispose Pattern](https://www.informit.com/articles/article.aspx?p=2756468&seqNum=7) • [Implementing IDisposable and the Dispose Pattern Properly](https://www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-P) –  Aug 04 '21 at 19:57
  • Just inject it and let the container handle it – GH DevOps Aug 04 '21 at 20:02
  • If you are making a Disposable class that you intend to subclass, you should follow a specific pattern (that involves creating an override of `Dispose` that takes a `bool` as an argument). Read this: https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose – Flydog57 Aug 04 '21 at 20:29

0 Answers0