Search code examples
c#destructordispose

Proper use of destructor c#


So I have been looking into implementing a destructor for a class I have written and I'm not sure how to really free up the memory or if this will be handled by garbage collection.

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
   }
}  

How the class is used:

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);
}  

So 4 objects are created and run, and this is done 20 times.
My question is how should I properly dispose/destruct these objects? I don't want to assume these are garbage collected, but I'm new to implementing desctructors.


Solution

  • As long as you don't use any objects that implement IDisposable, you shouldn't have to manually dispose or destruct.