所以我一直在考慮爲我寫的類實現一個析構函數,而且我不確定如何真正釋放內存,或者如果這將由垃圾回收處理。正確使用析構函數c#
class AutomatedTest
{
public bool testComplete = false;
public bool testStopRequest = false;
public List<Command> commandList = new List<Command>();
private bool loggingEnabled = false;
...
public AutomatedTest(TestList testToCreate)
{
// Create a list of Command objects and add them to the list
}
}
如何使用類:
for(int numTests = 0; numTests < 20; numTests++)
{
AutomatedTest test1 = new AutomatedTest(TestList._1DayTest);
RunAutoTest(test1);
AutomatedTest test2 = new AutomatedTest(TestList._2DayTest);
RunAutoTest(test2);
AutomatedTest test3 = new AutomatedTest(TestList._3DayTest);
RunAutoTest(test3);
AutomatedTest test4 = new AutomatedTest(TestList._4DayTest);
RunAutoTest(test4);
}
因此要創建和運行4個對象,這是做20次。
我的問題是我應該如何妥善處置/破壞這些對象?我不想假設這些垃圾已被收集,但我對實現解析器不熟悉。
可能重複的優選[析構VS IDisposable的?](http://stackoverflow.com/questions/456213/destructor-vs-idisposable) – BrokenGlass 2012-04-27 19:30:25