title: Extracting CSV Header data type
body: <p>In my current project, I am trying to derive the data type of CSV file. For instance, the following is the .CSV file.</p>
  
  <pre><code>sepallength,sepalwidth,petallength,petalwidth,class
  6.2,2.8,4.8,1.8,Iris-virginica
  6.3,2.9,5.6,1.8,Iris-virginica
  5.1,3.5,1.4,0.3,Iris-setosa
  5.2,3.5,1.5,0.2,Iris-setosa
  5.9,3,4.2,1.5,Iris-versicolor
  5.7,3,4.2,1.2,Iris-versicolor
  5.5,2.6,4.4,1.2,Iris-versicolor
  6.4,2.8,5.6,2.2,Iris-virginica
  </code></pre>
  
  <p>My requirement is that a program should give me the following output. </p>
  
  <ul>
  <li>"sepallength" is of "float" datatype </li>
  <li>"sepalwidth" is of "float" datatype </li>
  <li>"petallength" is of "float" datatype </li>
  <li>"petalwidth" is of   "float" datatype </li>
  <li>"class" is of "String" datatype </li>
  </ul>
  
  <p>I have written the following program. However the "<code>columnName.GetType()</code>" function always return <strong>string</strong> data type.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Data;
  using Microsoft.VisualBasic.FileIO;
  using System.IO;
  
  namespace ReadCSVFile
  {
      class Program
      {
          static void Main(string[] args)
          {
              // This is a file Path that a user has to input.
              string csv_file_path = @"C:\TestCSV\iris.csv";
              string columnName;
  
              DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);
  
              Console.WriteLine("Column Value of the CSV file are as follows:");
              Console.WriteLine("=========================================");
  
              // This will retrieve columnNames from the table.
              foreach (DataColumn column in csvData.Columns)
              {
                  columnName = column.ColumnName;
                  Console.WriteLine(columnName);
                  Console.WriteLine("Column type " + columnName.GetType());
              }
      }
  
      /*
       *  This function takes the file Path as an input and returns a Datatable.
      */
  
      private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
      {
          DataTable csvData = new DataTable();
  
          try
          {
              // Connect to the .CSV file Path
              using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
              {
                  csvReader.SetDelimiters(new string[] { "," });
                  csvReader.HasFieldsEnclosedInQuotes = true;
  
                  // This will read the column name of a .CSV file.
                  string[] colFields = csvReader.ReadFields();
  
                  foreach (string column in colFields)
                  {
                      DataColumn datecolumn = new DataColumn(column);
                      datecolumn.AllowDBNull = true;
                      csvData.Columns.Add(datecolumn);
                  }
  
                  // This code retrieves rows from the .CSV file.
                  while (!csvReader.EndOfData)
                  {
                      string[] fieldData = csvReader.ReadFields();
                      //Making empty value as null
                      for (int i = 0; i &lt; fieldData.Length; i++)
                      {
                          if (fieldData[i] == "")
                          {
                              fieldData[i] = null;
                          }
                      }
  
                      csvData.Rows.Add(fieldData);
                  }
              }
          }
          catch (Exception ex)
          {
          }
          return csvData;
      }
  </code></pre>