I have DisposableObject is IDisposable. I know that it should be wrapped to the using or dispose is explicitly called.
public void Method()
{
var disposable = new DisposableObject();
}
But I have argued about this logic with some colleagues. Do you think that 'unhanled' Dispose method will be called on automatically by GC on some point?
Thanks for the answer.
Is in this case Dispose method will be called automatically by GC?
No. It will not call Dispose
you have to call it yourself or use using
block which will ensure its disposal even in case of an exception.
using(var disposable = new DisposableObject())
{
//your code
}
IDisposable
is used to ensure that unmanaged resources held by the application are released prior to garbage collection. Since GC
can't release resources held by unmanaged resources like file handler.