title: How to write a C# List to CSV with CsvHelper package
body: <p>I have a simple Console Application that contains a list of 3 users in Program.cs. Within Program.cs I want to call a method in <code>FileOperations.cs</code>.</p>
  <p>First my <code>Program.cs</code>:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Text;
  using CsvHelper;
  
  namespace GimpiesConsoleOOcsvListUI
  {
      class Program
      {
          static void Main(string[] args)
          {
              // Create user default instances
              User user0 = new User(&quot;Beheer&quot;, &quot;beheer@gimpies.nl&quot;, &quot;123&quot;);
              User user1 = new User(&quot;Inkoop&quot;, &quot;inkoop@gimpies.nl&quot;, &quot;123&quot;);
              User user2 = new User(&quot;Verkoop&quot;, &quot;verkoop@gimpies.nl&quot;, &quot;123&quot;);
  
              // List of users (with a list you can add, get and remove items in the list)
              List&lt;User&gt; users = new List&lt;User&gt;();
              users.Add(user0);
              users.Add(user1);
              users.Add(user2);
  
              // Create login instance
              LoginManager loginMgr = new LoginManager();
  
              Start:
              // Welcome message
              Console.WriteLine(&quot;Welcome to GimpiesTerminal! Choose 1 to login or 2 to register:&quot;);
  
              // Get input from user
              string input = Console.ReadLine();
  
              bool successfull = false;
  
              while (!successfull)
              {
                  if(input == &quot;1&quot;)
                  {
                      Console.WriteLine(&quot;Enter your username:&quot;);
                      string username = Console.ReadLine();
                      Console.WriteLine(&quot;Enter your password:&quot;);
                      string password = Console.ReadLine();
  
                      foreach (User user in users)
                      {
                          if (username == user.UserName &amp;&amp; password == user.PassWord)
                              {
                                  Console.WriteLine(&quot;You have successfully logged in !!!&quot;);
                                  Console.ReadLine();
                                  successfull = true;
                              break;  
                              }
                      }
                          if (!successfull)
                              {
                                  Console.WriteLine(&quot;Your username or password is incorrect, try again !!!&quot;);
                              }
                  }
                  else if (input == &quot;2&quot;)
                  {
                          // Get user input
                          Console.WriteLine(&quot;Enter your username:&quot;);
                          string username = Console.ReadLine();
  
                          Console.WriteLine(&quot;Enter your email:&quot;);
                          string email = Console.ReadLine();
  
                          Console.WriteLine(&quot;Enter your password:&quot;);
                          string password = Console.ReadLine();
  
                          // Create fresh instance to save input in memory
                          User user = new User(username, email, password);
  
                          // Adds the user to the excisting list
                          users.Add(user);
                          
                          successfull = true;
                          goto Start;
                  }
                  else if (input == &quot;3&quot;)
                  {
                      // Here I want to call the method from FileOperations.cs to write the List here to a CSV file.
                  }
                  else
                  {
                      Console.WriteLine(&quot;Try again !!!&quot;);
                      break;
                  }
              }
              
              // // Loop over stored users within instances inside the list where users are added
              // foreach (User user in users)
              // {
              
              //     if(loginMgr.Login(user))
              //     {
              //         // Login successfull
              //         Console.WriteLine(&quot;Login user &quot; + user.UserName);
  
              //     }
              //     else
              //     {
              //         // Not successfull
  
              //     }
              // }  
              
          }
      }
  }
  </code></pre>
  <p>And here is my <code>FileOperations.cs</code>:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Text;
  using CsvHelper;
  
  namespace GimpiesConsoleOOcsvListUI
  {
      public class FileOperations
      {
          // Handles CRUD within CSV files and able to save them
          public void SaveToCSV(User user)
          {
              using (var mem = new MemoryStream())
              using (var writer = new StreamWriter(mem))
              using (var csvWriter = new CsvWriter(writer))
              {
                  csvWriter.Configuration.Delimiter = &quot;;&quot;;
                  csvWriter.Configuration.HasHeaderRecord = true;
                  csvWriter.Configuration.AutoMap&lt;User&gt;();
  
                  csvWriter.WriteHeader&lt;User&gt;();
                  csvWriter.WriteRecords(user);
  
                  writer.Flush();
                  var result = Encoding.UTF8.GetString(mem.ToArray());
                  Console.WriteLine(result);
              }
          }
      } 
  }
  </code></pre>
  <p>I get the following errors at line 16 of <code>FileOperations.cs</code>:</p>
  <p>StreamWriter writer
  Argument 1: cannot convert from <code>System.IO.StreamWriter</code> to <code>CsvHelper.ISerializer</code></p>
  <p>And line 23:</p>
  <p>User user
  Argument 1: cannot convert from <code>GimpiesConsoleOOcsvListUI.User</code> to <code>System.Collections.IEnumerable</code></p>
  <p>I'm new to C# so I hope you don't mind if it is a simple to solve problem! :-)</p>