I have bit of an odd question and I could't find a clear answer to this even though there are threads bordering around the same question.
Question:If I set an object to null, will that cause the dispose method (implemented) be called deterministically? For example in the following code, by setting pricingEnvironment object to null, will Dispose get called immedietly? I understand that the finalizer will kick off for pricingEnvironment object at some point if Dispose is not called though.
Code:
public interface IPricingService
{
double GetPrice(string instrument);
}
public interface IPricingEnvironment:IDisposable
{
void Initialize();
}
public class PricingEnvironment : IPricingEnvironment
{
public void Dispose()
{
DisposeObject();
}
public void Initialize()
{
//initialize something leaky
}
private void DisposeObject()
{
//release some leaky unmanaged resource
}
~PricingEnvironment()
{
DisposeObject();
}
}
public class PricingService:IPricingService, IDisposable
{
private IPricingEnvironment pricingEnvironment;
public PricingService()
{
pricingEnvironment = new PricingEnvironment();
}
public double GetPrice(string instrument)
{
pricingEnvironment.Initialize();
return 1d;
}
public void Dispose()
{
//Will this dispose the leaky resource used by pricing environment deterministically?
pricingEnvironment = null;
}
}
Thanks, -Mike