title: calling object members in c# foreach loop
body: <p>I have lot of tab pages in my program and I want to add or remove these pages. I'm using the following statement to do it.</p>
  
  <pre><code>if(this.myControlForm.TabPages.Contains(tabPage1))
  {
      this.myControlForm.TabPages.Remove(tabPage1));
  }
  if(this.myControlForm.TabPages.Contains(tabPage2))
  {
      this.myControlForm.TabPages.Remove(tabPage2));
  }
  [...] //similar code for other tabPages
  </code></pre>
  
  <p>but it makes too much code when I have a lot of tabpages to add or remove so I want to add them in an array and make a foreach loop but I got an error</p>
  
  <p>my code is :</p>
  
  <pre><code>object[] listToRemove = {tabPage1, tabPage2,tabPage3, ... tabPage20};
  
  foreach ( object itr in listToRemove)
  {
      if(this.myControlForm.TabPages.Contains(itr))
      {
          this.myControlForm.TabPages.Remove(itr));
      }
  }
  </code></pre>
  
  <p>Why is this happening, can anyone please help?</p>