0

I was following an post example where I have a service like MyService and was wrapped in the using keyword like

(using MyService = new MyService())
{

}

After building, the compiler complained about the service needing to implement IDisposable so I added

public class MyService : IDisposable
{
  void IDisposable.Dispose()
  {

  }
}

The compiler did not complained and the 'using' implementation seem to be working but I was told that is the wrong implementation. I got the IDisposable.Dispose() from one of the post here. Now most of the posts have implementions of add a public void Dispose(disposing).

I am looking for a easier way to Dispose versus multiple methods in a few services. Is there any easier way?

Thanks

c0micrage
  • 1,118
  • 2
  • 21
  • 56
  • `After building, the compiler complained about the service needing to implement IDisposable so I added`, if you don't need to implement `IDisposable`, then _don't_ wrap your service instantiation in a `using` statement. The _only_ purpose of the `using` block is syntactic sugar to ensure `Dispose()` is called at the end (even if an exception is thrown). – Pac0 Apr 29 '20 at 12:21
  • Visual Studio 2019 will happily auto-implement it for you if you just open the the code hints (Ctrl+., or click the light bulb, when the caret is on the line of the `Dispose()` method. – Pac0 Apr 29 '20 at 12:23
  • Possible duplicate of [Use of Finalize/Dispose method in C#](https://stackoverflow.com/questions/898828/use-of-finalize-dispose-method-in-c-sharp) – GSerg Apr 29 '20 at 12:26
  • *"but I was told that is the wrong implementation"* - ask them why. I don't see a problem with [implicit interface implementation](https://softwareengineering.stackexchange.com/q/136319/156546) as well as I don't see other code to judge. – Sinatr Apr 29 '20 at 12:30
  • 1
    @Sinatr The OP is using explicit interface implementation though. While it doesn't matter for the `using` clause, it is much more convenient to have `Dispose` available on the object itself. And, when this [short form of the Dispose pattern](https://stackoverflow.com/a/898867/11683) is used, the class should be marked as `sealed` which it is not. – GSerg Apr 29 '20 at 12:33

2 Answers2

3

If you are not using inheritance for these classes, you could make those classes sealed. This will stop the compiler complaining about that you should provide a virtual dispose method.

public sealed class MyService : IDisposable
{
    public void Dispose()
    {

    }
}

If you want this class to be inherited, you should provide a public virtual void Dispose() method, so the inherited classes are able to have a Dispose method. Then you have to implement the dispose(bool disposing) pattern.

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
-2

If you implement the IDisposable interface in your class, you really need to understand the purpose of it. I believe you currently lack this knowledge. I recommend to look into this link and read it througfully.

The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.

Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed.

IDisposable Interface

Community
  • 1
  • 1
Dmitri Tsoy
  • 561
  • 6
  • 21