title: C# Conway's Game Of Life
body: <p>I built a version of Conway's game of life as a C# Windows Form Application.
  The form contains a TableLayoutPanel and each cell of the table contains a form control.
  Initially, I had the following classes:</p>
  
  <p><strong>Grid</strong></p>
  
  <pre><code>int Rows
  int Cols  
  List&lt;Row&gt; RowList
  </code></pre>
  
  <p><strong>Row</strong>  </p>
  
  <pre><code>List&lt;Cell&gt; CellList
  </code></pre>
  
  <p><strong>Cell</strong>  </p>
  
  <pre><code>bool IsAlive
  Button b
  int ColIndex
  int RowIndex
  </code></pre>
  
  <p>The code builds up a grid object, taking the no. of rows and columns as arguments, then places each of the cells button controls onto the form in the relevant row &amp; column of the TableLayoutPanel.
  I finished developing the game and all worked ok. My maximum grid size is 50x50 and runs quite smoothly.<br>
  Then I thought about it and decided I'd be better using CheckBox control instead of Button controls. This would negate the need for the boolean IsAlive in the Cell class as I could just use the CheckBox checked property. I also decided there was no need for the cell object to contain a row and column index as I could determine these by getting the controls location in the TableLayoutPanel. This also made me think there was no need for the Row class. I could just have a Grid with a collection of Cells.<br>
  I re-wrote the application in the new way, thinking it would be a cleaner solution but now it runs much, much slower when it comes to performance.
  The main processing code is identical, except originally it had to do: </p>
  
  <pre><code>foreach (Row r in RowList)
  {
      foreach (Cell c in CellList)
      {
          // Do stuff
      }
  }
  </code></pre>
  
  <p>where now it just does:</p>
  
  <pre><code>foreach (Cell c in CellList)
  {
      // Do Stuff
  }
  </code></pre>
  
  <p>I can't understand why the second version is so much slower. Is it due to the collection of cells being much larger instead of broken down into rows? Or anything to do with the fact I'm using CheckBox objects instead of buttons?</p>
  
  <p>Any help would be much appreciated!</p>