Assuming i have the following classes:
Class MainClass
{
private OtherClass1;
MainClass()
{
OtherClass1 = new OtherClass1();
}
void dispose()
{
OtherClass1 = null;
}
}
class OtherClass1
{
private OtherClass2;
OtherClass1()
{
OtherClass2 = new OtherClass2();
}
}
class OtherClass2
{
}
If i instatiate MainClass and later call dispose method, does the OtherClass1 gets garbage collected (later on)? Or do i have first to clear the reference to OtherClass2?
In the code as provided, you don’t have to
nullanything, you can safely remove yourdispose()and all will be well.If your OtherClass1 and/or OtherClass2 are managed resources, ie they implement the
IDisposableinterface then your code is not good enough. You then will have to chain the Dispose: