title: Global objects vs multiple new objects?
body: <p>Say I was to have a method that required a small number of objects to be created during each run, and the method would be called multiple times, i.e. a method to delete a given row from a database. Would it be better to create a new object each time and call the Garbage Collector (or similar) to destroy it at the end, or reinitialise the value each time?</p>
  
  <p><strong>Example:</strong><br>
  Using new constructors each time:</p>
  
  <pre><code>private void RemoveFolder(string dir)
  {
      OleDbCommand cmd2 = connection.CreateCommand();
      OleDbParameter parameterC = new OleDbParameter();
      cmd2.Parameters.Add(parameterC);
      parameterC.Value = dir;
  
      cmd2.CommandText = "DELETE * FROM [Directories] WHERE Path = ?";
      cmd2.ExecuteNonQuery();
      cmd2.Dispose();
  }
  </code></pre>
  
  <p>Using a singular global variable (initialised within the constructor):</p>
  
  <pre><code>private void RemoveFolder(string dir)
  {
      parameterC.Value = dir;
  
      cmd2.CommandText = "DELETE * FROM [Directories] WHERE Path = ?";
      cmd2.ExecuteNonQuery();
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> When I say better, I mean "as-a-whole" during normal non-"mission-critical" programs, where a large increase in performance would trump a minor decrease in stability. </p>
  
  <p><strong>EDIT2</strong> </p>
  
  <p><strong>An example of calling a similar method multiple times</strong> </p>
  
  <p>(Note that this is for another method of mine, AddFolder)</p>
  
  <pre><code>foreach (DirectoryInfo directory in directories)
  {
      parameter.Value = directory.FullName;
      cmd.CommandText = "SELECT LastModified FROM Directories WHERE Path = ?";
      reader = cmd.ExecuteReader();
  
      while (reader.Read())
      {
          output += reader.GetString(0);
      }
  
      if (output == null)
      {
          Console.WriteLine("New Directory! " + directory.FullName);
          AddFolder(directory);
      }
      output = null;
      reader.Close();
   }
  </code></pre>