title: for csv convert List<class> into byte array
body: <p>I have a Action</p>
  <pre><code> public FileContentResult DownloadCSV()
      {
          var people = new List&lt;Person&gt; { new Person(&quot;Matt&quot;, &quot;Abbott&quot;), new Person(&quot;John&quot;,&quot;Smith&quot;) };
          string csv = &quot;Charlie, Chaplin, Chuckles&quot;;
          Extensions.ToCSV(new DataTable());
          return File(new System.Text.UTF8Encoding().GetBytes(csv), &quot;text/csv&quot;, &quot;Report123.csv&quot;);
      }
  </code></pre>
  <p>and a class</p>
  <pre><code>public static class Extensions
  {
      public static string ToCSV(DataTable table)
      {
          var result = new StringBuilder();
          for (int i = 0; i &lt; table.Columns.Count; i++)
          {
              result.Append(table.Columns[i].ColumnName);
              result.Append(i == table.Columns.Count - 1 ? &quot;\n&quot; : &quot;,&quot;);
          }
  
          foreach (DataRow row in table.Rows)
          {
              for (int i = 0; i &lt; table.Columns.Count; i++)
              {
                  result.Append(row[i].ToString());
                  result.Append(i == table.Columns.Count - 1 ? &quot;\n&quot; : &quot;,&quot;);
              }
          }
  
          return result.ToString();
      }
  }
  
  new System.Text.UTF8Encoding().GetBytes(csv)
  </code></pre>
  <p>create</p>
  <pre><code>string csv = &quot;Charlie, Chaplin, Chuckles&quot;
  </code></pre>
  <p>into byte array how to convert</p>
  <pre><code>var people = new List&lt;Person&gt; { new Person(&quot;Matt&quot;, &quot;Abbott&quot;), new Person(&quot;John&quot;,&quot;Smith&quot;) };
  </code></pre>
  <p>into byte array with formatted header for csv</p>