0

I know this question has probably been answered before but I couldn't for the life of me find a question with this answer already gvien.

I have 2 classes that I call dispose on 1 of them, I was just wondering do I have to explicitly call dispose on class B inside of A's dispose? Or would it automatically do it due to it being a base class of A already?

The reason it concerns me so much is Resharper (A vs extension) constantly gives me a message directed to it.

'A.Dispose()' hides inherited member 'B.Dispose()', Use the new keyword if hiding was intended. The keyword 'new' is required on 'Dispose' because it hides method 'void App.B.Dispose()'

class A : B, IDisposable
{
    public void Dispose() {
    }
}

class B : IDisposable
{
    public void Dispose() {
    }
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
ssk239fk
  • 29
  • 3
  • 4
    If A inherits from B, why mark A as IDisposable? A is _already_ disposable – maccettura Jan 05 '18 at 20:10
  • 2
    So when you actually ran the code and called dispose, what actually happened? Which methods ran? – Servy Jan 05 '18 at 20:10
  • @maccettura It changes the binding of the interface methods for objects of that type. – Servy Jan 05 '18 at 20:11
  • 1
    I guess you didn't get the OO concept very well... When you have a class `B` that implement `IDisposable` and create a class `A` that inherites from `B`, then `A` automatically "have" everything `B` provides (including the implementation of `IDisposable`). You don't need to reimplement the `Dispose` method on `A` – Diego Rafael Souza Jan 05 '18 at 20:13
  • You should make Dispose virtual, and extend it in the child class. – rokkerboci Jan 05 '18 at 20:22
  • 2
    @DiegoRafaelSouza You're just assuming that nothing specific to `A` needs to be disposed. That may well not be true. – Servy Jan 05 '18 at 20:24
  • @Servy you're right, in this specific scenario my words make sense, but on a real enviroinment it's better use a virtual implementation and overwrite it when needed. Despite this, I think it's clear that he is starting over this concepts and I don't think he will understand an architetural discussion for now – Diego Rafael Souza Jan 05 '18 at 20:32
  • This page contains all the info you need https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx – pmcilreavy Jan 05 '18 at 20:35
  • @DiegoRafaelSouza If you make the method virtual and override it then the base class implementation isn't run. – Servy Jan 05 '18 at 21:06
  • @Servy we booth know that it's not an absolute true. It's possible to call `base.Dispose()`. I don't know what you want to prove so strongly, but there's no reason to keep discussing, you got your point. Congratulations =) – Diego Rafael Souza Jan 06 '18 at 20:18
  • @DiegoRafaelSouza You can call the base method without making it virtual too. You call the base method by *calling the base method*, not by making it virtual. Whether it's virtual has nothing to do with whether the base method is called. – Servy Jan 07 '18 at 15:38

1 Answers1

-1

You could implement IDisposable as virtual in the base class, but this is a violation of the Liskov substitution principle. If you require derived classes to do their own disposal, you should do something like the following:

internal abstract class B : IDisposable
{
    public void Dispose()
    {
        DisposeImpl();
    }

    protected abstract void DisposeImpl();
}

internal sealed class A : B
{
    protected override void DisposeImpl()
    {
        //TODO.
    }
}
Adam Brown
  • 1,667
  • 7
  • 9
  • Why is B inheriting from A in your answer? – maccettura Jan 05 '18 at 20:35
  • Because I read the question once and then wrote the answer. I missed that the OP was reversed. – Adam Brown Jan 05 '18 at 20:36
  • 2
    You shouldn't answer questions that are obvious duplicates. – pmcilreavy Jan 05 '18 at 20:36
  • 1
    It isn't a clear duplicate - I think the linked issue is using the "official" dispose pattern, which is of very limited utility unless you're disposing unmanaged resources. Also, the accepted answer of the linked issue is a Liskov violation. – Adam Brown Jan 05 '18 at 20:39