title: Write list of objects to text file
body: <p>I've got a class Dish like this:</p>
  
  <pre><code>public class Dish
  {
      public Dish(int cost, string name)
      {
          Cost = cost;
          Name = name;
      }
  
      public int Cost { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>In the main form the user can enter the previous data.
  Then I create a list of "Dish":</p>
  
  <pre><code>public static List&lt;Piatto&gt; List = new List&lt;Piatto&gt;();
  </code></pre>
  
  <p>I created it in a static class, so I can access it from anywhere.
  As soon as an item get added to the list, I want to save it in a text file (.txt) so I tried to use this:</p>
  
  <pre><code>public static void SaveToTxt()
      {
          using (TextWriter tw = new StreamWriter(Path))
          {
              foreach (var item in Data.List)
              {
                  tw.WriteLine(item.ToString());
              }
          }
      }
  </code></pre>
  
  <p>The issue is that when I open the text file where I saved my list, I get "WindowsFormsApplication1.Dish".</p>
  
  <p>How can I save to a text file showing Cost and Name?</p>
  
  <p>P.s. I would like to save the list in a text file because it's easier for me to delete a line, something that I don't know how to do in binary.</p>
  
  <p>Thanks in advance.</p>
  
  <p>EDIT:</p>
  
  <p>Overriding the <code>ToString()</code> method worked fine. Thank you all for the answers!</p>