title: Strange Behavior of Form.Controls.OfType
body: <p>I have a Form that gets built at run time where the user enters some data and either submits the data or cancels. The Form consists of a TableLayoutPanel with controls on it.</p>
  
  <p>I add the Submit and Cancel buttons as follows.</p>
  
  <pre><code>TableLayoutPanel panel = new TableLayoutPanel
  {
         ColumnCount = 3,
         RowCount = gridObj.ColList.Count() + 1,
         Height = 800,
         Width = 800
   };
  
  //other controls for user input...
  
      Button submitButton = new Button();
      submitButton.Text = "Submit";
      submitButton.Click += (sender, EventArgs) =&gt; { SubmitClick(sender, EventArgs, gridObj); };
      panel.Controls.Add(submitButton, 0, CurrentRow);
      this.AcceptButton = (submitButton);
  
      Button CancelButton = new Button();
      CancelButton.Text = "Cancel";
      panel.Controls.Add(CancelButton, 3, CurrentRow);
      this.CancelButton = (CancelButton);
  </code></pre>
  
  <p>There is a case where it is necessary to remove the buttons from the form. I do so with the following code.</p>
  
  <pre><code>private void RemoveExistingButtons(TableLayoutPanel panel)
      {
          foreach (Control ctrl in panel.Controls.OfType&lt;Button&gt;())
          {
              panel.Controls.Remove(ctrl);
          }
      }
  </code></pre>
  
  <p>The way it looks to me this should find and remove each button from panel.
  However it only removes the submit button.
  The cancel button is not removed.</p>
  
  <p>I was playing around with it and by adding another foreach, the cancel button gets removed by the second foreach.</p>
  
  <pre><code>private void RemoveExistingButtons(TableLayoutPanel panel)
      {
          foreach (Control ctrl in panel.Controls.OfType&lt;Button&gt;())
          {
              panel.Controls.Remove(ctrl);
          }
          foreach (Control ctrl in panel.Controls.OfType&lt;Button&gt;())
          {
              panel.Controls.Remove(ctrl);
          }
      }
  </code></pre>
  
  <p>I've tested this method with other control types, and they successfully remove multiple controls with only one loop, the way it 'should' work.</p>
  
  <pre><code> private void RemoveExistingControls(TableLayoutPanel panel)
      {
          foreach (Control ctrl in panel.Controls.OfType&lt;Label&gt;())
          {
              panel.Controls.Remove(ctrl);
          }
          foreach (Control ctrl in panel.Controls.OfType&lt;TextBox&gt;())
          {
              panel.Controls.Remove(ctrl);
          }
      }
  </code></pre>
  
  <p>I'm quite confused about this. Am I overlooking something simple?</p>