title: c# Add a Column to end of CSV file
body: <p>Trying to add a column to the end of a cvs file, which will just count up the number of lines (or can all be the same number it doesn't really mater) as the main aim is to add a new column with some data in it.</p>
  
  <p>The sample CSV file I have has 3 columns with some random data in each of them.</p>
  
  <p>What I have at the moment is</p>
  
  <pre><code>    List&lt;string&gt; newColumnData = new List&lt;string&gt;() { "D" };
      List&lt;string&gt; lines = File.ReadAllLines(@"C:/CSV/test.csv").ToList();
  
      //add new column to the header row
      lines[0] += ",Column 4";
      int index = 1;
  
      //add new column value for each row.
      lines.Skip(1).ToList().ForEach(line =&gt;
      {
          //-1 for header
          lines[index] += "," + newColumnData[index - 1];
          index++;
      });
      //write the new content
      File.WriteAllLines(@"C:/CSV/test.csv", lines);
  </code></pre>
  
  <p>This however throws up an exception 
  "index was out of range must be nonnegative and less than the size of the collection"</p>
  
  <p>Any advice would be most welcome as always.</p>