0

I have a class with the following definition, and I want it to have an override of Dispose method:

EDIT

it turns out there is a Dispose() method, but it's not an override. I was expecting an override. This may be an invalid question.... but why isn't it an override?

public class MyClient : IMyClient2
{
    public void Dispose()
    {
        Dispose(true);
    }

    private void Dispose(bool disposing)
    {
        if(disposing)
        {
            Stop();
        }
    }
}

Where the interface it extends, and it's subsequent interface, are defined as:

public interface IMyClient2 : IMyClient
{
    // one or two methods
}

public interface IMyClient : IDisposable
{
    // a bunch of methods
}

I thought that implementing IDisposable would require that my class have the Dispose() override. But it doesn't and I don't know what I need to do to make it so that it does.

What do I need to do to be able to properly dispose of instances of my class?

DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • I don't really understand the problem, do you mean that you can compile this code without implementing IDisposable.Dispose()? That shouldn't be possible. – Jakub Szumiato Oct 25 '16 at 10:46
  • With the little code you have shown above `MyClient` has to implement `IDisposable`s `Dispose` method. If it does not and there is no compiler error then you have not shown all your code. – Igor Oct 25 '16 at 10:48
  • Well, it absolutely is possible. This is code that I brought down from the repo, built and ran. It runs fine. I thought that I'd use the handy IDisposable implementation to override the IDispose method to solve a problem I'm having, but to my surprise there was no Dispose override (ReSharper told me so). – DaveDev Oct 25 '16 at 10:49
  • @DaveDev - are you sure that is the hierarchy? Can you show the actual classes? Do you have some base classes along the way and not only interfaces? – Gilad Green Oct 25 '16 at 10:50
  • 1
    No it is not possible. You can't define an interface to be implemented on a type and then not implement/define those method(s) on your type. The code will not compile. You have not shown all your code. – Igor Oct 25 '16 at 10:50
  • what version of the .NET are you using ? Did you write an interface IDisposable somewhere else in your code ? – Martin Verjans Oct 25 '16 at 10:50
  • It isn't clear from the question or the comments thus far, but do you expect `IDisposable` or `Dispose()` to be special or different than other interfaces because of the circumstances under which `Dispose()` is called or the fact that many BCL classes are disposable? – Keith Payne Oct 25 '16 at 11:31

3 Answers3

2

If your class really implements these interfaces as in the question you should get a compilation error of

MyClass does not implement interface member IDisposable.Dispose()

enter image description here

Which will be solved the moment you add a public void Dispose() implementation


Update:

it turns out there is a Dispose() method, but it's not an override. I was expecting an override. This may be an invalid question.... but why isn't it an override?

override keyword is used when overriding an abstract or virtual method from a base class. When you are implementing an interface there is no need for the keyword. Stated by MSDN on the override keyword:

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

If instead of an interface you would extend a base class and the Dispose method would have an abstract or virtual keywords you could override it's implementation in the derived class.

See object's ToString() as an example:

public class Object
{       
    // Returns a String which represents the object instance.  The default
    // for an object is to return the fully qualified name of the class.
    // 
    public virtual String ToString()
    {
        return GetType().ToString();
    }    
}

And if in your class you will add a function of :

public override string ToString()
{
    // Add your implementation
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • this is the correct answer. I was searching for an override, but I couldn't find one. It turns out the Dispose() method is there, but I didn't see it. As I stated in a comment above, I just brought this down from the repo and I didn't see the method. Sorry for the hassle. – DaveDev Oct 25 '16 at 10:54
1

I thought that implementing IDisposable would require that my class have the Dispose() override. But it doesn't and I don't know what I need to do to make it so that it does.

Change the base class

public class MyClient : IMyClient2
{
    public virtual void Dispose()
    {
        Dispose(true);
    }
}

Your new type

public class MyClient2 : MyClient
{
    public override void Dispose()
    {
        // add your dispose information
        base.Dispose(true);
    }
}

This will allow types that derive from your MyClient type to also implement their own override of the Dispose method. It is not required that you implement Dispose in your overriden type. If you want that then you have to mark your MyClient class as abstract with an abstract Dispose method.

Change the base class

public abstract class MyClient : IMyClient2
{
    public abstract void Dispose()
    {
        Dispose(true);
    }
}

Your new type

public class MyClient2 : MyClient
{
    public override void Dispose()
    {
        // add your dispose information
        base.Dispose(true);
    }
}
Igor
  • 60,821
  • 10
  • 100
  • 175
0

I thought that implementing IDisposable would require that my class have the Dispose() override. But it doesn't and I don't know what I need to do to make it so that it does.

No. Interfaces do not make use of abstract/virtual/override keywords. The author has implemented the interface, not overriden any virtual methods.

Note there is no virtual/abstract in the IDisposable definition:

public interface IDisposable
{
    void Dispose();
}

why isn't it an override?

The author could have chosen to follow best practices and provide a protected virtual void Dispose(bool disposing) but chose not too.
I would guess he didn't expect you to ever want to extend the class.

 

What can you do now?

If you have access to alter the code then you could go ahead and implement IDisposable as you want to. E.g. https://msdn.microsoft.com/en-us/library/ms244737.aspx

If you can't alter the code then you still have options. You could use composition/decorator pattern to provide your own implementation of Dispose.

You could inherit from MyClient and then explicitly provide the implementation for Dispose yourself. When the object is disposed, it will be the method assosiated with the interface which is called.
This effectivly is the same as overriding. E.g.:

public class MyClient2 : MyClient
{
    void IDisposable.Dispose()
    {
        base.Dispose();
        //Do whatever you like here.
    }
}
Buh Buh
  • 7,443
  • 1
  • 34
  • 61