1

I've a class called MyClass which inherits from IDisposable.

public class MyClass : IDisposable
{
    public void Dispose()
    {
    // clean up
    }
}

I'm not sure who is going to use my class, but I want to make sure that whoever is using my class must call Dispose() method after he is done using MyClass.

How can I force the user who is using MyClass to call this Dispose() method?

bluekushal
  • 499
  • 6
  • 14
  • 1
    You can't unless using a finalizer. All it does is to give a nice built in way to help people clean after them. (like if putting it in a using clause). – Gilad Green Jul 07 '17 at 07:00
  • 1
    You've already done your bit - your implementing `IDisposable`. That's the signal to your consumers/various linting tools that to omit the call to `Dispose` is an error. – Damien_The_Unbeliever Jul 07 '17 at 07:03

0 Answers0