Since objects are created from the least received to the most withdrawn, so they need to be torn from the most received to received.
, . , , .
, , :
public class MyBase {
public List<SomeObject> list;
public MyBase(){
list = new List<SomeObject>();
list.Add(new SomeObject());
list.Add(new SomeObject());
list.Add(new SomeObject());
}
~MyBase() {
foreach (SomeObject obj in list) {
obj.Cleanup();
}
list.Clear();
}
}
- :
public class MyDerived : MyBase {
public MyDerived() {
foreach (SomeObject obj in list) {
obj.SomeProperty = new Handler();
}
}
~MyDerived(){
foreach (SomeObject obj in list) {
obj.SomeProperty.Cleanup();
}
}
}
, , . , .
( , IDisposable .)