I want to use IDisposable interface to clean any resource from the memory, that is not being used.
public class dispose:IDisposable
{
public void Dispose()
{
throw new NotImplementedException();
}
public void fun()
{
PizzaFactory _pz = new PizzaFactory(); //
}
}
I want to dispose pz object, when there is no ref to it exists. Please let me know how to do it.
That’s what the garbage collector is for. If all you’re worried about is reclaiming memory, let the garbage collector do it for you.
IDisposableis about reclaiming unmanaged resources (network connections, file handles etc). IfPizzaFactoryhas any of those, then it should implementIDisposable– and you should manage its disposal explicitly. (You can add a finalizer to run at some point after there are no more live references to it, but it’s non-deterministic.)